Sei sulla pagina 1di 31

Java Fundamentals

1
Java Introduction

 Origin of Java 1991


 Initially called Greentalk later renamed as Oak.
 Oak renamed to Java in 1995
 Developed by
a)James Gosling
b)Patrick Naughton
c)Mike Sheridan

2
Java Characteristics
 Features of Java language 
– Simple
– Object-Oriented
– Portable
– Platform independent
– Secured
– Robust
– Architecture neutral
– Interpreted
– High Performance
– Multithreaded
– Distributed
– Dynamic

3
Java Architecture

4
Java Architecture Cont.,
Five Phases of Java Programs:
Step1:Creation of java source code(.java)
Step2:Compile the java Source code(.class)
Step 3:Class Loader loads both user and Library classes into
memory.
Step 4:Byte code verifier validates all the byte code and do not
violates security restrictions
Step5:JVM reads byte code and translates into machine code for
execution

5
Java Execution
1)Java JDK(Java Development Kit)
2)Java JRE(Java Runtime Environment)
3)JVM (Java Virtual Machine)

6
Java Execution Cont.,
1)Java JDK(Java Development Kit)
• software development environment which is used to develop
Java applications and applets.
– Standard Edition Java Platform
– Enterprise Edition Java Platform
– Micro Edition Java Platform
• Some JDK Tools are:
– Javac,java,jadoc,appletviewer,jdb,javah

7
Java Execution Cont.,
2)Java JRE(Java Runtime Environment)
- set of software tools which are used for developing Java applications. It is
used to provide the runtime environment
a).jar files
3)JVM(Java Virtual Machine)
-It is called a virtual machine because it doesn't physically exist.
-It provides a runtime environment in which Java bytecode can be
executed.
• The JVM performs the following main tasks:
– Loads code
– Verifies code
– Executes code
– Provides runtime environment

8
Difference between JDK, JRE, and JVM

9
Java Language Basics
Java Path Setting:
a)Java Installed :
C:\Program Files (x86)\Java\jdk1.8.0_102\bin
b) Java Program Stored:
D:\myproject\hello.java
c) Path Setting:
D:\myproject>set path=“C:\Program Files (x86)\Java\jdk1.8.0_102\bin”

10
Simple Java Program
Running my first Java Program
import java.io.*;
class test
{
public static void main(String args[])
{
System.out.println(“HELLO”);
}
}
Compiling:
D:\myproject\javac test.java
Exceution:
D:\myproject\java test
HELLO
11
Command Line Argument
Definition:It is an information that directly follows the program's name on the command
line when it is executed. To access the command-line arguments inside a Java program
is quite easy. They are stored as strings in the String array passed to main( ).
Running Java Program using Command Line Argument
import java.io.*;
class test
{
public static void main(String asrgs[])
{
System.out.println(“HELLO”+args[1]);
}
}
Compiling:
D:\myproject\javac test.java
Exceution:
D:\myproject\java test srr college
HELLO college
12
Java Tokens
• A token is the smallest element of a program that is meaningful to the compiler.
Tokens can be classified as follows:
• Keywords:
– Keywords are pre-defined or reserved words in a programming language.
– Eg: int,for,if,break etc..,
• Identifiers:
– Identifiers are used as the general terminology for naming of variables,
functions and arrays. 
• Constants:
– Constants refer to fixed values.
– Eg: final int a=10;
• Special Symbols
[] () {} : , :* =
• Operators
+ - * / % < > <= >= etc..,

13
Java Data Types
Definition:
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 data types: The primitive data types include boolean, char, byte, short,
int, long, float and double.
• Non-primitive data types: The non-primitive data types include  Classes, 
Interfaces, and Arrays.

14
Java Data Types Cont.,
a)Java Primitive Data Types
• In Java language, primitive data types are the building blocks of data
manipulation. These are the most basic data types available in Java
language.
Eg:boolean data type,byte data type,char data type,short data type,int data
type,long data type,float data type,double data type.

• There are 8 types of primitive data types:


a)boolean data type-1 bit b)byte data type-1 byte
c)char data type-2 byte d)short data type-2byte
e)int data type-4 byte f)long data type-8 byte
g)float data type-4byte h)double data type-8byte

15
Java Data Types Cont.,
b)Java Non-Primitive Data Types
Non-Primitive data types refer to objects and hence they are
called reference types. Examples of non-primitive types include Strings,
Arrays, Classes, Interface, etc. Below image depicts various non-primitive
data types.
• Strings: String is a sequence of characters. But in Java, a string is an
object that represents a sequence of characters. The java.lang.String class
is used to create a string object.
• Arrays: Arrays in Java are homogeneous data structures implemented in
Java as objects.
• Classes: A class in Java is a blueprint which includes all your data.  A class
contains fields(variables) and methods to describe the behavior of an
object.
• Interface: Like a class, an interface can have methods and variables, but
the methods declared in interface are by default abstract (only method
signature, no body).
16
Java Operators
Java Operators:
Operator in Java is a symbol which is used to perform
operations. For example: +, -, *, / ,>,< etc.
– Arithmetic Operator,
– Logical Operators
– Relational Operator,
– Bitwise Operator,
– Ternary Operator
– Assignment Operator.
– Increment /Decrement operators

17
Java Flow Control
Definition:
-Control flow statements, change or break the flow of
execution by implementing decision making, looping, and
branching your program to execute particular blocks of code
based on the conditions.
-There are 3 types of control flow statements supported by
the Java programming language.
a)Decision-making statements /Selection:
b)Looping statements /Iteration :
c)Branching statements /Jump :

18
Java Flow Control Cont.,

19
Java Flow Control Cont.,
a)Decision-making statements /Selection:
– If, if else, nested if ,else if ,switch
b)Looping statements /Iteration :
– for, while, enhanced for ,do-while
Eg:
– int []a={10,20,30,40,50};
– for(int x :a)
{
System.out.println(x);
}
b)Branching statements /Jump :
– Break-break from current iteration
– continue--skip the current iteration,
– return-return from current state
20
Java Arrays
Definition:
• Java Arrays. Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
• To declare an array, define the variable type with square brackets.
• We have now declared a variable that holds an array of strings.
• Declaring Array Variables
– dataType[] a; // preferred way.
or
– dataType a[]; // works but not preferred way.
• Creating Arrays with memory
– dataType[] a = new dataType[arraySize];
• Creating and Initializing Array
-dataType[] a = {value0, value1, ..., value n};
• Types of Arrays
a)One Dimensionbal Array
b)Multi Dimensional Arrays

21
Java Arrays Cont.,

22
1D-Arrays
Definition:
• Array is the collection of similar type of data having contiguous memory
location. 
• Due to contiguous memory location the performance becomes fast. 
• The biggest disadvantage of array is, we can’t increase or decrease its size
after defining it. 
• Array is represented by an object.
• Array reduces the program size. For example if we want to store roll
number of 100 students then we have to create 100 variables. It will make
the program very large and difficult to handle. This problem can be solved
easily by using an array of size 100. 

23
1D-Arrays
Eg:
class ArrayExample 
{    
     public static void main(String args[])
           {
            int []a=new int[5];           //declaring and creating an array
  a[0]=5;
           a[1]=10;
              a[2]=15;
              a[3]=20;
              a[4]=25;
              //accessing and printing array elements
              for(int i=0;i<5;++i)
                     System.out.println(a[i]);                                           
}
} 24
2D-Arrays
Definition:
• A 2D array is an array of arrays.
• It is used to store table of values in the row and column
manner.

Declaring an 2D array,
• Syntax,
dataType [][]int=new int[size][size];
Eg:
int[][] a = new int[3][4];

25
2D-Arrays Cont.,

26
2D-Arrays Cont .,
Eg:
class Testarray3
{  
public static void main(String args[])
{   
//declaring and initializing 2D array  
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};  
for(int i=0;i<3;i++)
{  
  for(int j=0;j<3;j++)
{  
   System.out.print(arr[i][j]+" ");  
  }  
 System.out.println();  
}  
}}
Output:
123
245
445 27
Type Conversions
• When you assign value of one data type to another, the two types might not be
compatible with each other. If the data types are compatible, then Java will
perform the conversion automatically known as Automatic Type Conversion
and if not then they need to be casted or converted explicitly.
A) Widening or Automatic Type Conversion
– - If we want to assign a value of smaller data type to a larger data type we
perform explicit type casting or narrowing.
– byte->short->int->long->float->double
B) Narrowing or Explicit Conversion
– If we want to assign a value of larger data type to a smaller data type we
perform explicit type casting or narrowing.
– double->float->long->int->short->byte
Eg1:
int a=10;
long aa=a;//widening
Eg2:
double b=20;
float x=(float)b;//narrowing 28
Type Conversions Cont.,
String to int
• int i=Integer.parseInt("Wipro200");//200
int to string
• int j=10;
• String s=String.valueOf(i);//10 of String data types
String to long
• long l=ong.parseLong("Wipro9833445578")//9833445578
long to String
• long i=9833445578;
• String s=String.valueOf(i);//9833445578 of String type
String to float
• float f=Float.parseFloat("123");//123.0
String to char
• String s="ABC";
• System.out.println(s.charAt(0));
Char to String
• char c='H';
• String s=String.valueOf(c);//H of String Type
29
Type Conversions Cont.,
char to int
• char c='a';
• int a=c;//97
int to char
• int b=97;
• char c=(char)b;//a
Binary to Decimal
• System.out.println(Integer.parseInt(“1010”,2));//10
Decimal to Binary
• System.out.println(Integer.toBinaryString(10));//1010
Hexadecimal to decimal
• String h="b";
• System.out.println(Integer.parseInt(h,16));//11
Decimal to Hexadecimal
• System.out.println(Integer.toHexString(c));//12
Octal to Decimal
• String oct="121";
• System.out.println(Integer.parseInt(oct,8));//81(64+16+1)
Decimal to octal
• System.out.println(Integer.toOctalString(8));//10
30
THANK YOU

31

Potrebbero piacerti anche