You are on page 1of 5

Java Programming

Download Jdk: Java Development Kit

It is a software for building Java Applications, it has a compiler, it has a bunch of


code that we can run, it has a Java runtime environment.

After that, we will need a Code Editor, like NetBeans, IntelliJ, Eclipse.

The Anatomy of a Java Programs:

Functions: A Function is a block of code that performs a task. As a metaphor, think


about your tv control, each button performs a task, Functions in programming
language are exactly the same.

We start specifying the Returning type of this Function Returntype. Some Functions
return a value, like a number, day time and so on. Other functions don’t return
anything, so the Returntype of this Function is Void. Void is a reserved key word in
Java.

Then we have the name of our Function. We have to name our function with a
specified name like sendEmail, it’s clear that we are going to send an Email with this
Function. After that we have a pair of parentheses and inside these parentheses in
add the paremeters. We use these parentheses to give value to a Function. And
then we have the pair of curly braces and inside of these braces we write the actual
Java Code. We put the left brace on the same line where we define our function.

Main is the entry code to our programs.

A Class is a container for one or more related functions.


A Method is a function that is part of a Class.
Access Modifier is a special keyword that determines if other classes and methods
in this program can access these classes and methods.

public class Main {


public void main () {

}
}

At a minimum we have a Main class, and inside this Main class we have the Main
method.

Your first Java Program:

In Java textual data we always use double quotes: “”


String: is a sequence of characters .

public class Main {


public void main () {
System.out.println (“Hello World”);

}
}

Result: Hello World

How Java Code gets executed:


There are two steps involved, which are Compilation and Execution.

In the Compilation step, Netbeans uses the Java Compiler to compile our code into a
different format called Java bytecode.
We have four editions of Java for building different kinds of application.

We have Java Standard Edition, this is the core of the platform. It contains all of the
libraries that every Java developer must learn.
We have Java Enterprise Edition, which is used for building very large-scale and
distributed systems. It’s built on top of Java Standard Edition and provides additional
libraries for building fault tolerant, distributed multi-tiered software.
We have Java micro Edition, which is a subset of java Standard Edition designed
for mobile advices, it has libraries specific to mobile devices.
We have Java Card, which is used in smart cards

Variables: We use Variables to temporary store data in computer’s memory, to


store these variables we use integer (int).
Integers are whole numbers, numbers that don’t have a decimal point.
Then we give the variables a name, will be its identifier. Because we use to identify
our variable. The equal sign is called the assignment operator.

public static void main(String[] args) {


int myAge = 30
int herAge = myAge

System.out.println(herAge);
}

Result = 30

Compilers: It’s a computer program, will take your source code and then compile to
binary file.
Interpreters: Is a language processer which converts high-level programming to
low-level language.

Primitive Types:

Primitive types: for storing simple values


Non-Primitive types (References): for storing complex objects. Like data objects or
mail messages.
Primitive types:
we have byte that takes one memory and in one bite we can store values from -128
to 127. The more bytes we have, the larger numbers we can store.
Short: which takes 2 bytes and with this we can store values up to 32,000.
Integer: takes 4 bytes of memory and we can store values up to 2 billion.
Long: which takes 8 bytes of memory and it can store values up to 2 billion, if you
want to store values higher than 2B we have to add L at the end.
Float: used for decimal numbers (such as 3,2; 10,50… and so on). It takes 4 bytes
Add F at the end if it has an error message.
Double: used for decimal numbers (such as 3,2; 10,50… and so on). It takes 8 bytes
of memory
Char: for storing a single character like ABC, it takes 2 bytes of memory. To use this
character, we have to use just one quotes. Like this ‘A’. A string represents a series
of characters.
Boolean: for storing Boolean values which can be true or false.

Always use descriptive names for your variables, so you won’t be lost on your own
codes.

Reference Types:
Ex:

package helloworld;

import java.util.Date;
/**
*
* @author andregoncalves
*/
public class HelloWorld {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
byte age = 30;
Date now = new Date();
System.out.println(now);

Result: actual date.

Strings: The string is a string literal, that means a string value.


Strings are reference types in Java.
Strings in Java are immutable. We cannot mutate them, we cannot change them.

message.endsWith + … : check if a string ends with something mentioned before.


message.startsWith + … : check if a string starts with something mentioned before.
System.out.println(message.endsWith("H");
Result: true

message.lenght: to check the length of the message (how many characters has on
it). This is useful in situations where you want to check the length of the input by the
user.
message.indexOf: It gives you the index of what was written. Like for the Hello
World, if you put H, will appear 0 in the output because H is in number 0 position.
The index of the first position is 0.
message.replace: You can replace one or more characters or something else.

String message = "Hello World" + "!!";


System.out.println(message.replace("!", "*");
Result: Hello World** instead of Hello World!!

message.toLowerCase: it will give all the characters a lower case.


message.toUpperCase: it will give all the characters an UPPER case.

message.trim: it will get rid of all the extra spaces that was given in our code.

Escape Sequences: There will be times when we are going to use special
characters like “” double quotes or () strings in our code, like a tab, or a new line
or a / backslash.

We have String message = "Hello Mosh"; let’s say that we want surround Mosh with
double quotes, but there’s a problem if we add a quote, the Java compiler will think
that is the termination of our string, so it won’t understand what we have after. To fix
this problem, we have to prefix this double code with a backslash.

“Hello \”Mosh\””

We use Enum for things that don’t change, instead of using an array we user
enum, which is cleaner.

You might also like