Professional Documents
Culture Documents
Java INTRO1
Java INTRO1
What is Java?
System.out.print(“Hello, World”);
Ex:
//this is a comment - single-line comment
/*this is a comment*/ - multi-line
/** documentation */ - Javadoc
Java Variable
- A variable is a container that holds values that are used in a Java program. Every
variable must be declared to use a data type.
- a variable could be declared to use one of the eight primitive data types.
- every variable must be given an initial value before it can be used.
Ex:
<data type> <variable name> [=initial value];
int a= 9;
int x;
Note: values enclosed in <> are required values, while those values in [] are optional.
Rules
VALID INVALID
$sum 1sum
exer2exer Exer-1
StudInfo Exer,exer
Stud_Info Sum/
void
static
Data Type
Example:
Note: values enclosed in <> are required values, while those values in [] are
optional.
Guidelines in declaring and initializing
variables:
System.out.print();
System.out.println();
Example:
public class VarOutput{
public static void main(String args[]){
int value = 10;
char x;
x = „A‟
System.out.println(value);
System.out.println(“The value of x = ” + x);
}
}
Simple Assignment Operator
Example:
Assume integer variable A holds 10 and variable B
holds 20 then:
Operato Description Example
r
+ Addition - Adds values on either side of the operator A + B will give
30
- Subtraction - Subtracts right hand operand from left hand A - B will give -
operand 10
/ Division - Divides left hand operand by right hand operand B / A will give 2
% Modulus - Divides left hand operand by right hand operand B % A will give
and returns remainder 0
If statement
If else
If else if
switch
If statement
If the boolean expression evaluates to true then the block
of code inside the if statement will be executed. If not the
first set of code after the end of the if statement(after the
closing curly brace) will be executed.
Syntax:
if(Boolean_hexpression) {
//Statements will execute if the Boolean expression is true
}
public class Test {
public static void main(String args[]){
int x = 10;
if( x < 20 ){
System.out.print("This is if statement");
}
}
}
If. . .else
An if statement can be followed by an
optional else statement, which executes when the Boolean
expression is false.
Syntax:
if(Boolean_expression){
//Executes when the Boolean expression is true
}
else{
//Executes when the Boolean expression is false
}
public class Test {
public static void main(String args[]){
int x = 30;
if( x < 20 ){
System.out.print("This is if statement");
}else{
System.out.print("This is else statement");
}
}
}
If else if
if(Boolean_expression1){
//Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
//Executes when the Boolean expression 3 is true
}else {
//Executes when the none of the above condition is
true.
}
public class Test {
public static void main(String args[]){
int x = 30;
if( x == 10 ){
System.out.print("Value of X is 10");
}else if( x == 20 ){
System.out.print("Value of X is 20");
}else if( x == 30 ){
System.out.print("Value of X is 30");
}else{
System.out.print("This is else statement");
}
}
}
Nested if...else Statement
It is always legal to nest if-else statements, which means you
can use one if or else if statement inside another if or else if
statement.
Syntax:
if(Boolean_expression 1)
{
//Executes when the Boolean expression 1 is true
if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}
}
public class Test {
public static void main(String args[]){
int x = 30;
int y = 10;
if( x == 30 ){
if( y == 10 ){
System.out.print("X = 30 and Y = 10");
}
}
}
}
The switch Statement:
for(initialization;Boolean_expression;update
){ //Statements
}
Example
while(Boolean_expression) {
//Statements
}
The do while loop:
Syntax:
do{
//statements
}while(expression);
Array
Array
One-dimensional Array
Array1[j]
Two-dimensional Array
Array1[ i ][ j ]
One-Dimensional Array
It is the basic array
A vertical table with number of columns and
only one row
Index and Length
Indices – used to access each location of arrays from 0
ranging up to the instantiated array
Two-Dimensional Array
It is an array of an array
Also called as tables or matrices
Row – first dimension
Column – second dimension
Two-Dimensional Array
It is an array of an array
Also called as tables or matrices
Row – first dimension
Column – second dimension
Syntax:
type arrayName [ row ][ column ];
A two-dimensional array can be think as a table, which will
have x number of rows and y number of columns. A 2-
dimensional array a, which contains three rows and four
columns can be shown as below:
a[3][4]
Initializing Two-Dimensional
Arrays:
Example:
int a[3][4] = { {0, 1, 2, 3} ,
{4, 5, 6, 7},
{8, 9, 10, 11}};