AVA Asics: Tanjina Helaly
AVA Asics: Tanjina Helaly
Tanjina Helaly
TOOLS/SET-UP
STEP1: INSTALL JAVA AND PATH SET-UP
Need to install Java(JDK and
JRE). Get the latest version from
Java Standard Edition(SE) from
http://www.oracle.com/technetwork
/java/javase/downloads/index.html
After installing Java you need
to set-up the “Path”
environment variable which is
available from My Computer
under Advanced Properties
tab.
Note: Do not delete anything in
“Path” variable. Just add your
path “C:\Program
Files\Java\jdk1.8.0_31\bin;”
(Depending on your version the
path will change) at the
beginning of the existing value.
STEP 2: INSTALL IDE
Need an IDE: Eclipse or NetBeans or IntelliJ IDEA.
Or
A Text Editor e.g. TextPad
You can install
eclipse from :
http://www.eclipse.org/downloads/packages/eclipse-ide-
java-developers/mars1
NetBeans:
http://www.oracle.com/technetwork/java/javase/downloads/i
ndex.html
IntelliJ IDEA:
https://www.jetbrains.com/idea/download/#section=window
s
COMPILE & RUN JAVA APPLICATION
WITHOUT IDE
Using JDK you can compile and run java
program from command line.
c:> javac HelloWorld. Java
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 JDK is not user friendly. IDE
is provided for that. A list of IDEs are:
Eclipse
Netbeans.
IntelliJ IDEA
DATA TYPES
Divided into two broad categories:
primitive types
class/reference types.
Example
public class TestCast {
public static void main(String[] args) {
byte b= 5;
int a = b; // OK. Auto Casting
byte c = a; // Compiler error. Need Casting
c = (byte)a; // Casting
float f = 1.2f;
a= f; // Compiler error. Need Cast
a = (int)f; // Explicit Cast
f = a;
}
}
OPERATOR
Assignment =
Arithmetic + - * / %
Equality == !=
Logical &&, ||
increment/decrement ++ --
Initialization
During declaration
int[] sampleArray = {1,2,3,4,5};
After declaration
int[] sampleArray;
sampleArray = new int[]{1,2,3,4,5};
sampleArray = {1,2,3,4,5}; // compiler error
ARRAY SIZE & ACCESSING A SPECIFIC
INDEX
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;
Loop
for
while
do-while
CONTROL STATEMENT
“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
JUMP 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.
JUMP STATEMENT
public class BreakExample { Output:
public static void main( String args[] ) { Outer loop: 0
for ( int row = 0; row < 5; row++ ) { 0 Break
System.out.println("Outer loop: " + Outer loop: 1
row); 0 1 Break
for ( int column = 0; column < 4 ; Outer loop: 2
column++ ) {
0 Break
System.out.print(column +" " );
Outer loop: 3
if ( ((row + column) % 2 ) == 0 ) {
0 1 Break
System.out.println("Break " );
Outer loop: 4
break;
} 0 Break
}
}
}
}
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); Output:
for ( int column = 0; column < 4; Outer loop: 0
column++ ) { 0 Break
System.out.println(column + "\t");
if ( ((row + column) % 2 ) == 0 )
{
System.out.println("Break " );
break Outer;
}
}
}
}
}
JUMP 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