JAVA Tutorials


Java is a popular programming language, created in 1995. It is used for: 1.Mobile applications (specially Android apps) 2.Desktop applications 3.Web applications 4.Web servers and application servers 5.Games 6.Database connection install the Java in our pc and set the path; step 1 : Advanced system settings. step 2 : Advance Tab. step 3 : Click "Environment Variables" Button. step 4 : Click "New.." button under "User variables for.." step 5 : Enter "PATH" in the Variable Name. and enter your java installation location in variable value Ex: "C:\Program Files (x86)\Java\jdk1.6.0\bin"; Then Click OK. Finally Go to cmd and type java and also type javac



Save as MyTest.java

public class MyTest {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}
Main Method
public static void main(String[] args)
System.out.println() This is use for display output



Variables are containers for storing data values. In Java, there are different types of variables, for example: String - stores text, such as "World". String values are surrounded by double quotes int - stores integers (whole numbers), without decimals, such as 1234 or -1234 float - stores floating point numbers, with decimals, such as 18.91 or -11.98 char - stores single characters, such as 'b' or 'D'. Char values are surrounded by single quotes boolean - stores values with two states: true or false Declaring Variables

type variable = value;
Example:
String name = "yohan";
System.out.println(name);
Data Type
int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99f;    // Floating point number
char myLetter = 'D';         // Character
boolean myBool = true;       // Boolean
String myText = "Hello";     // String



Operators are used to perform operations on variables and values. 1. + operator to add together two values Example:

int x = 150 + 40;
2. - operator to Subtracts together two values Example:
int x = 150 - 40;
Arithmetic Operators
Operator Name Description Example
+ Addition Adds together two values x + y
- Subtraction Subtracts one value from another value x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value from another value x / y
% Modulus Returns the division remainder value x % y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x

Assignment operators
Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

Comparison operators
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Logical Operators
Operator Name Description Example
&&  Logical and Returns true if both statements are true x < 5 &&  x < 10
||  Logical or Returns true if one of the statements is true x < 5 || x < 4
! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)



if Statement Syntax

if (condition) {
  // code to be executed if the condition is true
}
Example :
if (25 > 12) {
  System.out.println("20 is greater than 18");
}

int x = 20;
int y = 18;
if (x > y) {
  System.out.println("x is greater than y");
}
else Statement
Syntax
if (condition) {
  //  code to be executed if the condition is true
} else {
  // code to be executed if the condition is false
}
Example:
int time = 22;
if (time < 10) {
  System.out.println("Good morning.");
} else if (time < 20) {
  System.out.println("Good day.");
} else {
  System.out.println("Good evening.");
}
// Outputs "Good evening."



The switch statement to select one of many code blocks to be executed. Syntax

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}
This is how it works:
1. The switch expression is evaluated once.
2. The value of the expression is compared with the values of each case.
3. If there is a match, the associated block of code is executed.
4. The break and default keywords are optional, and will be described later in this chapter

Example:
int day = 4;
switch (day) {
  case 1:
    System.out.println("Monday");
    break;
  case 2:
    System.out.println("Tuesday");
    break;
  case 3:
    System.out.println("Wednesday");
    break;
  case 4:
    System.out.println("Thursday");
    break;
  case 5:
    System.out.println("Friday");
    break;
  case 6:
    System.out.println("Saturday");
    break;
  case 7:
    System.out.println("Sunday");
    break;
}
// Outputs "Thursday" (day 4)



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

while (condition) {
  // code block to be executed
}
Example:
int i = 0;
while (i < 8) {
  System.out.println(i);
  i++;
}



When you know exactly how many times you want to loop through a block of code, using the for loop instead of a while loops: Syntax

for (initialize variable1; condition 2; increment ordecrement 3) {
  // code block to be executed
 }
Ex.
for (int i = 0; i <= 10; i = i + 2) {
  System.out.println(i);
}



There is also a for-each loop, is used exclusively to loop through elements in an array: Syntax

for (type variableName : arrayName) {
  // code block to be executed
}
Ex:
String[] Vehicle= {"Car", "Van", "Bus", "Lorry"};
for (String i : Vehicle) {
  System.out.println(i);
}



Arrays are used to store more values in a single variable, instead of declaring separate variables for each values. Syntax variableType variableName[] = value Ex:

String[] cars;
String[] cars = { "BMW",  "Mazda","Ford","Volvo"};
int[] myNum = {20, 10, 40, 80};

System.out.println(cars[0]);
Array indexes start with 0: [0] is the first element. [1] , etc.

Array Length
Ex:
String[] amimal= {"cat", "rat", "dog", };
System.out.println(amimal.length);
// Outputs 3

Array Loop
Ex:
String[] amimal= {"cat", "rat", "dog", };
for (int i = 0; i < amimal.length; i++) {
  System.out.println(amimal[i]);
}


Comments


Post Comments

  • 0 comments