Java Fundamentals
Java Fundamentals
SESSION 1
Java Fundamentals
01
CONTENTS
Introduction-Java Architecture
Data types
Variables
2
Introduction-Java
Architecture
3
● Java, a Programming Language
developed by Sun Micro System in
the year 1991.
● Java was developed by James
Gosling, Patrick Naughton, Chirs
Warth, Ed Frank and Mike Sheridon.
● During the initial period Java was
called as Oak (from 1991 to 1995).
4
● Java is an object oriented
programming language. It has
syntax similar to C and C++
languages, but has vastly improved
features.
● Secure, Portable and Platform
Independent language
-Windows, Mac, Linux
5
JAVA FEATURES
Object-oriented
Secure
Platform Independent/Portable
Architecture-neutral
Robust
6
Object
O
Object
Object
Object
OBJECT
ORIENTED
Object Oriented Programming Language-Everything in
Java is treated as Objects and how they interact with
each other.
7
No explicit pointer
Java Programs run inside a virtual machine
sandbox.
Classloader in Java is a part of the Java
Runtime Environment (JRE) which is used to
load Java classes into the Java Virtual Machine
dynamically. It adds security.
9
It uses strong memory management.
Java provides automatic garbage
10
Java is architecture neutral because there are no
ARCHITECTURE dependent features, for example,
11
Basic Steps To Develop a Java Program
12
Java Architecture
13
Java Architecture
Step1: Create a java source code with .java extension
Step2: Compile the source code using java compiler, which will
create bytecode file with .class extension.
Step3: Class loader reads both the user defined and library
classes into the memory for execution.
Step4: Bytecode verifier validates all the bytecodes are valid
and do not violate Java’s security restrictions.
Step5: JVM reads bytecodes and translates into machine code
for execution. While execution of the program the code will
interact to the operating system and hardware.
14
5 Phases of Java Program
Edit: Use an editor to type Java program (Welcome.java)
Compile: Use a compiler to translate Java program into an
intermediate language called bytecodes, understood by Java
interpreter (Welcome. Class)
Loading: Use a class loader to read bytecodes from .class file into
memory
Verify :Use a Bytecode verifier to make sure bytecodes are valid
and do not violate security restrictions
Execute: Java Virtual Machine (JVM) uses a combination of
interpretation and just-in-time compilation to translate bytecodes
into machine language
Applications are run on user's machine, i.e. executed by interpreter
with java command (java Welcome) 15
A Simple Java Program
16
Java Fundamentals
17
Basics
Data Types
Keywords
Variables
Operators
Conditional Statements
Loops
18
Datatypes and
Keywords
19
Datatypes
Data Size Minimum Range Maximum Range Default Value
Type (in (for fields)
bits)
21
Datatypes
What will be the result, if we try to compile and
execute the following code?
class Test {
public static void main(String ar[]) {
float f=1.2;
boolean b=1; error: incompatible types: possible lossy conversion
from double to float
System.out.println(f); float f=1.2;
System.out.println(b); error: incompatible types: int cannot be converted to
} boolean
boolean b=1;
}
22
Datatypes
class execute
What will be the result, if we try to compile and Test {
public static void main(String ar[]) {
the following code?
double d=1.2D;
class Test {
public static void main(String ar[]) { System.out.println(d);}
}
double d=1.2;
System.out.println(d);}
}
class Test {
public static void main(String ar[]) {
double d=1.2d; Output:1.2
System.out.println(d);}
}
23
Datatypes Conversion-Promotion
24
Datatypes Conversion-Casting
25
Keywords
abstract continue for new switch
26
Keywords
What will be the result, if we try to compile and execute the
following code?
class Test {
public static void main(String [ ] ar)
{
Error
int for=128;
System.out.println(b);
}
}
27
Variables
28
Variables
A variable is a container which holds the
value. A variable is assigned with a data
type. Variable is a name of memory
location.
29
Variables
Variable Naming Conventions
Begin with a alphabet or _ not with a digit
Begin each variable with a lowercase letter
Subsequent words should be capitalized:
− myVariable
Choose names that are mnemonic and that indicate the intent of
the variable to the casual observer
Remember that …
−Names are case-sensitive
−Names can’t include white space
30
Input and Output
31
Output Methods
System.out.print():
• This method prints the text on the console and the cursor remains at
the end of the text at the console.
• The next printing takes place from just here.
• This method must take at least one parameter else it will throw an
error.
32
Output Methods
System.out.println():
• This method prints the text on the console and the cursor remains at
the start of the next line at the console.
• The next printing takes place from the next line.
• This method may or may not take any parameter..
Java
Java
Java
33
Output Methods
System.out.printf():
• This method prints the text on the console and the cursor remains at the
start of the next line at the console.
• The next printing takes place from the next line.
• This method may or may not take any parameter.
34
Getting Input from Keyboard
The Scanner class in Java is located within the java.util library.
To collect input from the keyboard in Java, there are several
methods available, with the java.util.Scanner being one of the
notable ones.
Commonly utilized for breaking down text into strings and various
basic data types, the Java Scanner class leverages regular
expressions to accomplish this.
Acquiring user input in Java is most straightforward when using
the Scanner class.
Java's Scanner allows the acquisition of user data in fundamental
data types like integers, long integers, doubles, bytes, floats, shorts,
and more.
35
Getting Input from Keyboard
36
Getting Input from Keyboard
Step 2: To get Inputs of different datatypes
String next() It is used to get the next complete token from the scanner which is
in use.
BigDecimal nextBigDecimal() It scans the next token of the input as a BigDecimal.
BigInteger nextBigInteger() It scans the next token of the input as a BigInteger.
boolean nextBoolean() It scans the next token of the input into a boolean value and
returns that value.
byte nextByte() It scans the next token of the input as a byte.
double nextDouble() It scans the next token of the input as a double.
float nextFloat() It scans the next token of the input as a float.
int nextInt() It scans the next token of the input as an Int.
String nextLine() It is used to get the input string that was skipped of the Scanner
object.
long nextLong() It scans the next token of the input as a long.
37
Getting Input from Keyboard
Example
38
Thank you!
Write a closing statement or call-to-action here.
39