Day 1 Java Fundamentals PDF
Day 1 Java Fundamentals PDF
E d
IT hin
EL Ma
Introduction to Java
ch
Te
ra
Download and install Java
E d
Understand the Java architecture
IT hin
Compile and run Java applications
Understand the language fundamentals
EL Ma
Use the control structures in programs
Use arrays and strings
ch
Write programs using command line arguments
Te
Introduction to Java
ra
Java Architecture
E d
Setting up the environment
IT hin
Language Fundamentals
Data types and operators
EL Ma
Control Structures
Arrays and Strings
ch
Command line arguments
Te
ra
• A deployment environment
E d
IT hin
Similar in syntax to C++; similar in semantics to Smalltalk
EL Ma
Operating system independent ch
Runs on Java Virtual Machine (JVM)
Te
ra
Robust
E d
Architecture neutral
IT hin
Portable
EL Ma
Secure
High performance ch
Interpreted
Te
ra
Once compiled, java code runs on any platform without recompiling or any
E d
kind of modification
IT hin
“Write Once Run Anywhere”
EL Ma
Java Virtual Machine (JVM) made this possible
ch
Te
ra
E d
IT hin
EL Ma
ch
Te
ra
JSR $817
E d
CPX #0
Translation
IT hin
BNE #14
program 00010100
(Assembler)
EL Ma
Assembly language program 001100101
00001000
100100101
ch
010101010
Te
10010
Machine language program
(executable file)
#include <stdio.h>
ra
0001010000
E d
1100101000
IT hin
main() 0100010010
{ C Compiler 01010101010
EL Ma
printf(“Hallo”); 1010010
}
ch
Te
Class Loader
ra
Source File (HelloWorld.java)
E d
IT hin
Byte Code Verifier
EL Ma
JIT Code
ch
Compiler (javac) Interpreter
Generator
Te
Runtime
Byte code
(HelloWorld.class) Operating System
Hardware
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited
Copyright © 2013 Tech Mahindra. All rights reserved. 101
0
Write Once Run Anywhere!
public class Hello{
public static void main(String[] args){
System.out.println("Hello World!");
}
ra
}
Hello.java
E d
IT hin
javac (java compiler)
EL Ma
Interpreter Interpreter Interpreter Interpreter
ch
Te
Bytecode (Class)
Hello.class
ra
E d
IT hin
Byte code is in binary language to be interpreted by JVM
EL Ma
Also performs strong type checking, prevents access violations etc.
ch
Te
ra
Is implemented as software and is specific to each platform
E d
IT hin
The JVM interprets the .class file to the machine language of the underlying
EL Ma
platform
ch
The underlying platform then processes the commands given by the JVM
Te
ra
(Java Language)
Class Foo{
E d
.
IT hin
.
…main(..){ Virtual 3. Execute It
EL Ma
}
} Machine
Linux
ch
Intermediate
Te
Requires more memory because both byte code and the corresponding native
ra
E d
code are in memory at the same time.
IT hin
EL Ma
ch
Te
ra
JDK 1.0 Oak January 1996
E d
JDK 1.1 (none) February 1997
IT hin
J2SE 1.2 Playground December 1998
EL Ma
J2SE 1.3 Kestrel May 2000
J2SE 1.4 Merlin February 2002
ch
J2SE 5.0 Tiger September 2004
Te
ra
Includes the command-line Java compiler (javac) and the Java Runtime
E d
IT hin
Environment (JRE)
EL Ma
The JRE provides the runnable Java platform which supplies the java
command needed to execute Java applications
ch
Download and install JDK 7 from
Te
http://www.oracle.com/technetwork/java/javase/downloads/index.html
ra
UNIX export JAVA_HOME=/var/usr/java
E d
IT hin
CLASSPATH: Used to locate class files
EL Ma
set CLASSPATH=%CLASSPATH%;%JAVA_HOME%\lib\tools.jar
ch
Windows
Te
ra
class Hello {
E d
IT hin
public static void main(String[] args) {
EL Ma
System.out.println(“Hello World”);
ch
}
Te
}
Compilation:
C:\> javac Hello.java
Running your first java program:
C:\> java Hello
Copyright © 2013 Tech Mahindra. All rights reserved. 19
Try it out
1. The Java compiler for Windows and Linux are same (True/False)
ra
E d
3. Java compiler translates .java source files into ___________?
IT hin
a) .exe file b) .bat file c) .doc file d) .class file
EL Ma
4. _________ converts the bytecode to native code.
a) JRE b) JVM c) JIT d) JDK
ch
Te
ra
E d
A multi line comment starts with /* and ends with */
IT hin
/* This is a
EL Ma
Multi line
Comment in Java */
ch
Te
ra
E d
boolean do if private this
IT hin
break double implements protected throw
EL Ma
byte else import public throws
case enum
ch instanceof return transient
Te
ra
Classes, methods and variables cannot have Java Keywords as Identifiers
E d
IT hin
(names).
EL Ma
true, false and null are literals (not keywords), but they can’t be used as
ch
identifiers as well.
Te
ra
Example :
E d
int myAge=28, cellPhone;
IT hin
double salary;
char tempChar;
EL Ma
Variables can be declared anywhere in the program
ch
for (int count=0; count < max; count++) {
int z = count * 10;
Te
If a local variable is used without initializing, the compiler will show an error.
The below is an error
int tempVal;
System.out.println(tempVal);
ra
Holds value
E d
IT hin
Reference data type
EL Ma
Holds reference to objects
Example
ch
int primitive = 5;
Te
Memory Representation
primitive 5
reference Hello
CONFIDENTIAL© Copyright 2008 Tech 26
Mahindra Limited
Copyright © 2013 Tech Mahindra. All rights reserved. 26
Primitive Data Types
Data Size Type Signed Default Min Value Max Value
Type (bits) Value
ra
char 16 Single ‘\u0000’ ‘\u0000’ (0) ‘\uFFFF’ (216 – 1)
E d
Unicode
IT hin
Character
byte 8 Integer 0 -128(-27) 127 (27 – 1)
EL Ma
short 16 Integer 0 -215 215 – 1
-231 231 – 1
int 32 Integer
ch 0
long 64 Integer 0L -263 263 – 1
Te
Append uppercase or lowercase "L" or "F" to the number to specify a long or a float number.
Copyright © 2013 Tech Mahindra. All rights reserved. 27
Unicode Character Set
char data type in Java is 2 bytes because it uses UNICODE character set
to support internationalization
ra
E d
UNICODE character set supports all known scripts and languages in the
IT hin
world
EL Ma
ch
Te
ra
Object creation and cast new (type) Right to Left
E d
Multiplication/Division/Modulus */% Left to Right
IT hin
Addition/Subtraction +- Left to Right
Shift >> >>> << Left to Right
EL Ma
Relational < <= > >= instanceof Left to Right
Equality == != Left to Right
ch
Bit-wise/Boolean AND & Left to Right
Te
ra
bigger capacity. This is called widening
int i = 10;
E d
double d;
IT hin
d = i;
EL Ma
Legal conversions are
byte short int long float double
ch
Te
char
double d = 10
Type cast operator
int i;
i = (int) d;
Copyright © 2013 Tech Mahindra. All rights reserved. 30
Control Structures
Work the same as in C / C++
if/else, for, while, do/while, switch
ra
E d
IT hin
EL Ma
ch
Te
A break statement will cause the current iteration of the innermost loop to stop
ra
and the next line of code following the loop to be executed.
E d
IT hin
A continue statement will cause the current iteration of the innermost loop to
EL Ma
stop, and the condition of that loop to be checked, and if the condition is met,
perform the loop again. ch
Te
ra
E d
The length property on arrays tells the size of the array
IT hin
EL Ma
An array holding 5 int elements
22 33 66 100 72
ch
Te
ra
Allocated (constructed)
E d
IT hin
a = new int[10];
b = new String[arraysize]
EL Ma
Initialized
for (int i = 0; i < a.length; a[i++] = 0)
ch
Te
ra
To declare a multidimensional array, specify each additional index using
E d
another set of square brackets
IT hin
int [][] x;
EL Ma
//x is a reference to an array of int arrays
ch
x = new int[3][4];
//Create 3 new int arrays, each having 4 elements
Te
//x[0] refers to the first int array, x[1] to the second and so on
//x[0][0] is the first element of the first array
//x.length will be 3
//x[0].length, x[1].length and x[2].length will be 4
ra
All String operations (concat, trim, replace, substring etc) construct and return
E d
new strings.
IT hin
String objects are immutable. If modified, Java creates a new object having
EL Ma
the modified character sequence
ch
String myName = “Elliot Koffman”;
myName = “Koffman, Elliot”;
Te
ra
E d
char letter = 'a';
IT hin
String string1 = "Hello"; // String literals are stored as String objects
EL Ma
String string2 = "World";
String string3 = "";
ch
String dontDoThis = new String ("Bad Practice"); // Use is discouraged
Te
}
}
ra
String string3 = "";
E d
IT hin
string3 = "Hello".concat(string2);
System.out.println("string3: " + string3);
EL Ma
// Get length
ch
System.out.println("Length: " + string1.length());
Te
// Get SubString
System.out.println("Sub: " + string3.substring(0, 5));
// Uppercase
System.out.println("Upper: " + string3.toUpperCase());
}
}
Copyright © 2013 Tech Mahindra. All rights reserved. 38
Arrays and for-each loop
public class ArrayOperations {
public static void main(String args[]){
ra
String[] names = new String[3];
E d
IT hin
names[0] = "Blue Shirt";
names[1] = "Red Shirt";
EL Ma
names[2] = "Black Shirt";
ch
int[] numbers = {100, 200, 300};
Te
ra
This data is passed to the application in the form of String arguments
E d
IT hin
class Echo {
public static void main (String args[]) {
EL Ma
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
ch
}
}
Te
ra
2. Array index starts at ___. Index is of ____ data type.
E d
a) 1,int b) null, char c) 0,int
IT hin
EL Ma
3. Java supports pointer arithmetic. (True/False)
ra
Java2 platform and its components
E d
IT hin
Java language fundamentals
Identifiers & Literals
EL Ma
Primitive Data Types & their Conversion
ch
Operators & basic flow controls in java
Arrays & Strings
Te
ra
Visit us at www.techmahindra.com
E d
IT hin
EL Ma
Disclaimer
Tech Mahindra Lim ited, herein referred to as TechM provide a w ide array of presentations and reports, w ith the
ch
contributions of various professionals. These presentations and reports are for inform ational purposes and private
circulation only and do not constitute an offer to buy or sell any securities mentioned therein. They do not purport to be a
com plete description of the m arkets conditions or developm ents referred to in the m aterial. While utm ost care has been
Te
taken in preparing the above, we claim no responsibility for their accuracy. We shall not be liable for any direct or indirect
losses arising from the use thereof and the viewers are requested to use the inform ation contained herein at their ow n risk.
These presentations and reports should not be reproduced, re-circulated, published in any media, website or otherw ise, in
any form or m anner, in part or as a whole, w ithout the express consent in writing of TechM or its subsidiaries. Any
unauthorized use, disclosure or public dissem ination of inform ation contained herein is prohibited. Unless specifically
noted, TechM is not responsible for the content of these presentations and/or the opinions of the presenters. Individual
situations and local practices and standards m ay vary, so viewers and others utilizing inform ation contained w ithin a
presentation are free to adopt differing standards and approaches as they see fit. You m ay not repackage or sell the
presentation. Products and names mentioned in m aterials or presentations are the property of their respective ow ners and
the mention of them does not constitute an endorsement by TechM. Inform ation contained in a presentation hosted or
prom oted by TechM is provided “as is” w ithout w arranty of any kind, either expressed or im plied, including any w arranty of
merchantability or fitness for a particular purpose. TechM assumes no liability or responsibility for the contents of a
presentation or the opinions expressed by the presenters. All expressions of opinion are subject to change w ithout notice.