Nothing Special   »   [go: up one dir, main page]

Java JDK and Intro

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

First of all you need to download and install your java JDK to compile and run the codes

JDK= Software Development Kit

https://www.oracle.com/br/java/technologies/javase-downloads.html
Follow the steps(MAC or Windows) to install your java JDK.
Java is a programming language.
Java Tutorial Java is used to develop mobile apps, web apps,
desktop apps, games and much more.
Java Quickstart
In Java, every application begins with a class name, and that class must match the filename.
Let's create our first Java file, called P1_Hello.java, which can be done in any text editor (like
Notepad).
The file should contain a "Hello World" message, which is written with the following code:

Note: Java is
case-
sensitive:
"MyClass"
and
"myclass"
has different
meaning.
Java Identifiers
All Java variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum,
totalVolume).
Note: It is recommended to use descriptive names in order to create understandable
and maintainable code:
Integer Types
Byte
The byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer
types to save memory when you are certain that the value will be within -128 and 127:

Example
byte myNum = 100;
System.out.println(myNum);

Short
The short data type can store whole numbers from -32768 to 32767:
Example
short myNum = 5000;
System.out.println(myNum);

Int
The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our
tutorial, the int data type is the preferred data type when we create variables with a numeric value.
Example
int myNum = 100000;
System.out.println(myNum);
Long
The long data type can store whole numbers from -9223372036854775808 to
9223372036854775807. This is used when int is not large enough to store the value. Note that
you should end the value with an "L":
Example
long myNum = 15000000000L;
System.out.println(myNum);

Float
The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you
should end the value with an "f":
Example
float myNum = 5.75f;
System.out.println(myNum);

The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note
that you should end the value with a "d":
Example
double myNum = 19.99d;
System.out.println(myNum);
Booleans
A boolean data type is declared with the boolean keyword and can
only take the values true or false:
Example
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
Use the else statement to specify a block of code to be executed if the condition is false.
The else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Java Switch Statements
Use the switch statement to select one of many code blocks to be executed.
Loops
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more readable.

Java While Loop


The while loop loops through a block of code as long as a
specified condition is true:

The Do/While Loop


The do/while loop is a variant of the while loop. This
loop will execute the code block once, before checking if
the condition is true, then it will repeat the loop as long
as the condition is true.
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
The example below will print the numbers 0 to 4:
For-Each Loop
There is also a "for-each" loop, which is
used exclusively to loop through
elements in an array:
Method Overloading
With method overloading, multiple methods can
have the same name with different parameters:
Overriding in Java
In any object-oriented
programming language,
Overriding is a feature that
allows a subclass or child
class to provide a specific
implementation of a
method that is already
provided by one of its
super-classes or parent
classes. When a method in
a subclass has the same
name, same parameters or
signature, and same
return type(or sub-type)
as a method in its super-
class, then the method in
the subclass is said
to override the method in
the super-class.
Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
To declare an array, define the variable type with square brackets:
String[] cars;

Access the Elements of an Array


You access an array element by referring to the index
number.
This statement accesses the value of the first element
in cars:

Change an Array Element


To change the value of a specific
element, refer to the index number:
Array Length
To find out how many elements an array has, use the length property:

Loop Through an Array


You can loop through the array elements with the for loop, and use
the length property to specify how many times the loop should run.
The following example outputs all elements in the cars array:
Loop Through an Array with For-Each
There is also a "for-each" loop, which is used
exclusively to loop through elements in arrays:
We can also use a for loop inside another for loop to get the elements of
a two-dimensional array (we still have to point to the two indexes):
Example

public class Main {


public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < myNumbers.length; ++i)
{
for(int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]); }
}

} }
End

You might also like