0% found this document useful (0 votes)
4 views

2. Java Basics

Uploaded by

mainul684islam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

2. Java Basics

Uploaded by

mainul684islam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Java Basics

CSE 1115: Object Oriented Programming

1
Compile and Run Java
Application
Without IDE

 Using J D K you can compile and run java


program from command line.
⚫ c:> javac HelloWorld. J ava
 compiling here and
 it will produce HelloWorld.class i.e. bytecode.

⚫ c:>java HelloWorld
 It runs java byte code on native machine
With Java IDE

 Creating, Compiling, Debugging and Execution


for these four steps J D K is not user friendly.
I D E is provided for that. A list of IDEs are:
⚫ Eclipse
⚫ Netbeans.
⚫ IntelliJ I D EA
Data Types
 Divided into two broad categories:
 primitive types
 class/reference types.

 Primitive data : eight types


⚫ Logical: boolean (true or false)
⚫ doesn’t hold integer (unlike C)
⚫ Textual: char (16 bits)
⚫ use the Unicode(International: 0-255) not ASCII(1 byte: 0-127)
⚫ Integral: byte (8 bits), short (16 bits), int (32 bits), and long (64 bits)
⚫ Floating point: float (32 bits) and double (64 bits)

 Class or reference data: two types


⚫ Textual: String
⚫ All classes that declare by yourself
Casting
 Converting from one data type to another.
 e.g. assigning an int value to a long variable
Casting
 float f = 3.72f;
 In Java, decimal numbers (like 3.72) are interpreted

as double by default.
 without the f suffix, Java will treat 3.72 as a double

and result in a type mismatch.


Casting
 Converting from one data type to another.
 e.g. assigning an int value to a long variable
 Example
public class TestCast {
public static void main(String[] args) {
byte b= 5;
int a = b; // O K . Auto C asting
byte c = a; // Compiler error. Need Casting c
= (byte)a; // C asting

float f = 1.2f;
a= f; // Compiler error. Need Cast
a = (int)f; // E x plicit C ast
f = a;
}
}
Operator

 Assignment =
 Arithmetic + - * / %

 Equality == !=
 Relational < <= > >=

 Logical & & , | |

 increment/decrement ++ --

 Shift << >>


Operator

int x = 5; // Binary:
0000 0101
int result = x << 1; // Shift left by 1 bit
System.out.println(result); // Output: 10 (Binary: 0000 1010)
Arrays
 An array is a collection of data items, all of the
same type, accessed using a common name.
 The data type can be either a primitive data

type or a reference type.

 Major differences with C/C++ arrays:


⚫ J ava arrays are references
⚫ J ava arrays know their size (length property)
⚫ J ava multidimensional arrays need not be
rectangular
⚫ J ava array elements are initialized
Array Declaration &
Initialization
 Declaration  Initialization
int[] sampleArray; ⚫ During declaration
sampleArray = new int[10]; int[] sampleArray = {1,2,3,4,5};
Or
int[] sampleArray = new
int[10];
⚫ After declaration
int[] sampleArray;
sampleArray = new int[]{1,2,3,4,5};
sampleArray = {1,2,3,4,5}; // compiler
error
 Declaration Difference
int [ ] sampleArray1, sampleArray2;  Both arrays
int sampleArray1[ ], variable;  Second one variable
Accessing a Specific Index
 Getting size of array
int[] sampleArray = new int[10];
int size = sampleArray.length; //this will return the size of the
array, here 10

 Accessing a specific item


⚫ Assigning a value
sampleArray[0] = 5;
sampleArray[1] = 2;
sampleArray[2] = 3;

⚫ Getting/Reading a value
int value = sampleArray[2];
Arrays – Example Code
public class ArrayExample
{
public static void main( String args[] )
{
// space to store Reference is allocated, no array space allocated
double[] sampleArray;

//allocate array locations on heap


sampleArray = new double[ 10 ];

// Indexing starts at 0 like C/C+


+
sampleArray[ 0 ] = 5.5;

// R eference refers to new array.


// O ld array available for
garbage collection
sampleArray = new double[ 2 ];
}
Dimensional Array

 multidimensional arrays are actually arrays of


arrays.
int twoD[][] = new int[4][5];
 Do not need to be rectangular

 During creation it’s required to specify the size

for the first/leftmost dimension. You can


allocate the remaining dimensions separately.
int twoD[][] = new int[4][];
Dimensional Array
Rectangular Irregular Array
Declarion & int twoD[][] = new int[4][5]; int twoD[][] = new int[4][];
Array or twoD[0] = new int[1];
Creation int twoD[][] = new int[4][]; twoD[1] = new int[2];
twoD[0] = new int[5]; twoD[2] = new int[3];
twoD[1] = new int[5]; twoD[3] = new int[4];
twoD[2] = new int[5];
twoD[3] = new int[5];
Example of 01234 0
Array 56789 12
11134 345
56789 6789
Dimensional Array
Irregular Array
int twoD[][] = new int[4][];
twoD[0] = new int[]{0};
twoD[1] = new int[]{1,2};
twoD[2] = new int[3]{3,4,5}; //error
twoD[3] = new int[]{6,7,8,9};
0
12
345
6789
Control Statements

 if –else
 switch

 Loop

⚫ for
⚫ while
⚫ do-while
Control Statements
 “Enhance for” or “for-each”
⚫ automatically cycles through an array in sequence from
the lowest index to the highest.
⚫ Syntax : for(type itr-var : collection) statement-block
⚫ Example:
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
for(int x: nums)
sum += x;
⚫ Advantage: Avoid boundary error
Printing Multi-dimensional
Array

public static void main( String args[] ) {


int twoD[][]={{1,2,3},{5,6}};
for(int[] oneD : twoD){
for(int element : oneD){
System.out.print(element+" ");
}
System.out.println();
}
}
Break Statement

 break
⚫ Exits out of a loop or switch statement
⚫ Unlabeled break exits out of the innermost loop or
switch
⚫ Use labeled break to exit out of nested loops or switch
or block.
⚫ In case of labeled break program exits from that
particular labeled block
Break Statement
public class BreakExample {
public static void main( String args[] ) {
for ( int row = 0; row < 5; row++ ) {
System.out.println("Outer loop: " + row);
for (int column = 0; column < 4 ; column++) {
System.out.print(column +" ");
Output:
if ( ((row + column) % 2 ) == 0 ) { Outer loop: 0
System.out.println("Break " ); 0 Break
break; Outer loop: 1
} 0 1 Break
} Outer loop: 2
} 0 Break
Outer loop: 3
}
0 1 Break
}
Outer loop: 4
0 Break
Break Statement
public class BreakExample {
public static void main( String args[] ) {
outerLoop: // Label immediately before the loop
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.println(i +“, “+ j);
if (i == 1 && j == 1) {
break outerLoop; //Breaks out of the outer Output:
loop 0, 0
} 0, 1
} 0, 2
} 0, 3
System.out.println("Exited both loops."); 0, 4
1, 0
}
1, 1
}
Jump Statement – Labeled
Jump
public class BreakExample {
public static void main( String args[] ) {
Outer:
for ( int row = 0; row < 5; row++ ) {
System.out.println("Outer loop: " + row);
for(int column = 0; column < 4;column++){

System.out.println(column + "\t");
if ( ((row + column) % 2 ) == 0 ) Output:
{ Outer loop: 0
System.out.println("Break "); 0 Break
break Outer;
}
}
}
Program will exit from the labelled block
}
of Outer which contains a nested loop
}
public static void main(String[] args) {
Outer:{
System.out.println("hello");
for (int i = 1; i < 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.println(i + " " +
j);
if (i == j) {
System.out.println("break
");
break Outer;
}
Output:
} Hello
} 11
int x = 10; break
System.out.println(x); out

}
System.out.println("out");
}
Continue Statement
 continue
⚫ A continue statement skips to the end of the current
loop's body.
⚫ The loop's boolean expression is then evaluated.
Code Output

public class TestContinue { 01


public static void main(String args[]) { 23
for(int i=0; i<10; i++) { 45
System.out.print(i + " "); if (i 67
%2 == 0) continue; 89
System.out.println("");
}
}
}

You might also like