You are on page 1of 40

RAJALAKSHMI ENGINEERING COLLEGE

OOPs Using JAVA


DAY 1
CONTENTS
– Introduction of JAVA
• Java Architecture
• Data types
• Variables
• Operators
• IO
Introduction of Java
● 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
Evolution of Java
● 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).
Design Goal
● Java was originally meant to be a platform-neutral
language for embedded software in devices.
● The goal was to move away from platform and OS-
specific compilers that would compile source for a
particular target platform to a language that would be
portable, and platform-independent.
Basic Steps To Develop a Java Program
Java Architecture (Contd.).
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
A Simple Java Program
The 5 phases of Java Programs
1. Edit
Use an editor to type Java program (Welcome.java)
2. Compile
• Use a compiler to translate Java program into an intermediate
language called bytecodes, understood by Java interpreter
(javac Welcome.java)
• Use a compiler to create .class file, containing bytecodes
(Welcome.class)
3. Loading
Use a class loader to read bytecodes from .class file into memory
The 5 phases of Java Programs(Contd.).
4. Verify
Use a Bytecode verifier to make sure bytecodes are valid and do
not violate security restrictions
5. 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)
Java Virtual Machine
Language Basics
● Keywords
● Data Types
● Variables
● Operators
● Conditional Statements
● Loops
Java Keywords
Data Types
Data types specify the different sizes and values that can
be stored in the variable. There are two types of data types
in Java:

• Primitive types
-Directly contains values
• Reference types
-References to objects
Primitive Data Types
Variables
A variable is a container which holds • int a; //declaration
the value. A variable is assigned
with a data type. Variable is a name
• a = 20; //initialization or
of memory location. assignment
• int b=0; //declaration and
Ex: int num=10;
initialization

10

num
Variables
Program to find the sum of two numbers

public class sum{


public static void main(String args[]){
int a = 20;
int b= 40;
int c = a + b;
System.out.println(c);
}
Operators
Java provides a set of operators to manipulate operations.
Types of operators in java are,
● Arithmetic Operators
● Unary Operator
● Relational Operators
● Logical Operators
● Simple Assignment Operator
● Bitwise Operators
Arithmetic Operators
Unary Operators
Relational Operators
Relational Operators
Logical Operators
Logical Operators
Simple Assignment Operator
Which assigns right hand side value to left hand side variable

Ex:

int a;
a = 10;
Bitwise Operators
Command Line Arguments
Passing command line arguments
class Simple {
static public void main(String[] args) {
System.out.println(args[0]);
}
}
Compile : javac Simple.java
Execute : Java Simple ECE ECE
Accessing numeric command line arguments
class Simple {
static public void main(String[] args) {
int i1 = Integer.parseInt(args[0]);
int i2 = Integer.parseInt(args[1]);
System.out.println(i1+i2);
}
} 30
Compile : javac Simple.java
Execute : Java Simple 10 20
Finding length of an Array
class Simple {
static public void main(String[] args) {
int len = args.length;
System.out.println(len);
}
}
5
Compile : javac Simple.java
Execute : Java Simple A B C D E
IO
Java 1.5 introduced the Scanner class which simplifies console
input.
It can also be used to read from files and Strings.
Scanner is available in the java.util package.

Scanner
Class
Scanner Class
 The Scanner class parses input from the source into tokens by using
delimiters to identify the token boundaries.
- Example: John,Tim,Adam,Jack
- Token 1: John Token 2: Tim Token 3: Adam
 The default set of delimiters consists of the whitespace characters,
space, tab, newline and carriage return.
 Scanner will read a line of input from its source.

Syntax:
Scanner <object name>= new Scanner(System.in);
Scanner Class
Scanner Class
QUIZ
Which of the following is used to load a .class file?
A. Class Loader
B. Byte Code Verifier
C. JIT Compiler
D. Interpreter

A
QUIZ
What will be the result, if we try to compile and execute the
following code?
class Test {
public static void main(String [ ] ar) {
int for=2;
System.out.println(for);
}
}
Error
QUIZ
What will be the result, if we try to compile and execute the
following code?
class Test {
public static void main(String [ ] ar) {
int a=2 + 5.0;
System.out.println(a);
}
}
Error
QUIZ
What will be the result if you try to compile and execute the following
code without passing any command line argument?
class Sample {
public static void main(String [ ] args) {
int len = args.length; d
System.out.println(len);
}
}
a. Compilation Error b. Runtime Error
c. The program compiles and executes successfully but prints nothing.
d. The program compiles and executes successfully & prints 0.

You might also like