SlideShare a Scribd company logo
UNIT I
OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Object Oriented Programming Paradigm - Basic
Concepts of Object Oriented Programming –
Benefits and Applications of OOP – Java Features –
Java Program Structure – Command Line
Arguments – Variables and Data Types – Operators
– Expressions – Arrays - Control Statements –
Selection – Iteration - Jump Statements
OBJECT ORIENTED PROGRAMMING
FUNDAMENTALS
• Object=Data + Methods
Features of OBJECT ORIENTED
PARADIGMS
1. Data
2. Objects
3. Data structures
4. Methods
5. Hidden data
6. Communication
7. Addition of new data
8. Bottom up approach
Basic concepts of OBJECT ORIENTED
PROGRAMMING
I. OBJECTS & CLASSES
II. DATA ABSTRACTION & ENCAPSULATION
III. INHERITANCE
IV. POLYMORPHISM
V. DYNAMIC BINDING
VI. MESSAGE COMMUNICATION
I.OBJECTS & CLASSES
• Object-Basic runtime entity
• Class- Collection of objects of similar type
II.DATA ABSTRACTION &
ENCAPSULATION
• Encapsulation–Wrapping up of data and
methods into single unit(called class).
• Data hiding –Insulation of data from direct
access by program.
• Data abstraction Representing essential
features only.
III.INHERITANCE
• Objects of 1 class acquire the properties of
objects of another class.
IV.POLYMORPHISM
• Ability to take more than one form
V.DYNAMIC BINDING
• Linking of procedure call to code.
• Linking of method call to its corresponding
method definition takes place only during
runtime.
VI.MESSAGE COMMUNICATION
There are 3 steps.
• Class creation
• Object creation
• Establish communication
BENEFITS OF OOP
• Inheritance
• Time saving and high productivity.
• Data hiding
• Multiple objects
• Mapping of objects
• Easy to upgrade
• Communication through messages
• Can manage software complexity easily
APPLICATIONS OF OOP
• Real time systems.
• Simulation & Modelling.
• Object oriented DB
• AI
• Neural networks
JAVA FEATURES
Introduction:-
• Java is an object oriented language developed
by Sun Microsystems.
• Original name was OAK . Later renamed to
JAVA.
• Platform neutral language(platform
independent.)(Java Virtual Machine).
• Java is both compiled and interpreted
language.
Compilation Process
Execution Process
Programming in java  basics
JAVA FEATURES
1. Java is both a compiled and an interpreted language.
2. Platform independent and portable-Write Once Run
Anywhere.
3. Object Oriented.
4. Robust and Secure.
5. Distributed.
6. Automatic Memory Management.
7. Dynamic binding
8. High performance.
9. Multithreading.
Some features in C/C++ are eliminated in java.
They are listed below.
• No Pointers
• No Preprocessor Directives
• No Multiple Inheritance in Classes
• No operator overloading.
• No global variables.
Simple Java Program
class Welcome
{
public static void main(String args[])
{
System.out.println(“Welcome to Java world “);
}
}
JAVA PROGRAM STRUCTURE
Simple Java Program
/* * To change this template, choose Tools | Templates and open the template in the
editor. */
package college;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql;
class sum
{
public static void main(String args[])
{
int i=10,j=25;
k=i+j;
System.out.println(“THE SUM IS “+k);
}
}
Download JDK and install in
Windows10- Use the following
YouTube link.
https://video.search.yahoo.com/search/video?fr
=mcafee&ei=UTF-
8&p=download+jdk&type=E211US1045G0#id=4
&vid=3aa873b105c9f56e6a73b619da6222ba&a
ction=click
Compilation code- javac filename.java
Execution code- java filename
JAVA TOKENS
• In Java every statement is made up of smallest
individual elements called tokens. Tokens in java
include the following:-
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Separators
1. Keywords
• Keywords cannot be redefined by a
programmer
• Keywords cannot be used as identifier name.
Eg: Abstract, Assert, Boolean, Break, Byte, Case.
• Java language has reserved 50 words as
keywords.
2. Identifiers
• Programmer designed tokens. Used for naming
variables, methods, classes etc.
• An identifier can contain alphanumeric
characters, but it should begin with an alphabet.
• Identifiers are case sensitive.
• Underscore and dollar sign are two special
character allowed.
• Eg:A23C
• Bob@gmail - Wrong
3. Literals
• LITERAL is a program element that represents
an exact (fixed) value of a certain type. Java
language supports the following type of
literals.
1. Integer Literals. (Ex. 123)
2. Floating point Literals. (Ex. 88.223)
3. Character Literals. (Ex. ‘.’ , ‘a’ , ‘n’)
4. String Literals. (Ex. “Hello World”)
5. Boolean Literals. (Ex. True,false)
4. Separators
• Symbols used to indicate where group of code
are divided and arranged.
• ()
• {}
• []
• ;
• ,
• .
5. Operators
• Tells the computer to perform certain
mathematical or logical manipulations on data
and variables. Different types are:-
1. Arithmetic Operators.
2. Unary Operators.
3. Assignment Operators.
4. Equality and Relational Operators.
5. Logical and Conditional Operators.
6. Bitwise and Bit Shift Operators.
7. Special operators
1. Arithmetic Operators
Eg: a+b
a-b
a*b
a/b
a%b
2. Unary Operators
Eg: +b
-b
++b
--b
3. Assignment Operators
Eg:a=b+c;
a=a+1; and a+=1; same meaning.
a+=1 is shorthand assignment operator.
4. Equality and Relational Operators
Eg: a==b;
a!=b;
• Value of relational expression is either true or false.
• It is used in decision statements.
Eg: a<b;
a>b;
a<=b;
a>=b;
5. Logical and Conditional Operators.
• Used to form compound conditions by combining 2 or more
relations.
&& -logical AND
|| -logical OR.
! – logical compliment.
Eg: a>b && x==10;
?: - Conditional ternary opeartor(shorthand for if-then-else
statement).
Eg: x=(a>b)?a:b;
6. Bitwise and Bit Shift Operators
~ Unary bitwise complement
<< Signed left shift
>> Signed right sift
>>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
7. Special Operators
1. Instanceof operator
Eg: person instanceof student.
• Checks whether object person belongs to class student or not.
• Returns either true or false
2. Dot operator /member selection operator
• Used to access variables and methods of a class object.
Eg: Person1.age;
Person1.salary();
Where person1 is an object .
Age is a variable
Salary() is a method.
Example:Operators
• public class StringLowerUpperExample{
• public static void main(String args[]){
• String s1="JAVATPOINT HELLO stRIng";
• String s1lower=s1.toLowerCase();
• System.out.println(s1lower);
• String s2="hello string";
• String s1upper=s2.toUpperCase();
• System.out.println(s1upper);
• }}
Operator Precedence Table
Operator Precedence
Operators Precedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
EXPRESSIONS
• Java expressions are used to implement some computation.
• Syntax: Variable=Exprerssion;
• Simple Expression : x=a+b;
• Complicated expression : a*(b+c)/(m+n)*f;
In this example,
1. (b+c) or (m+n) executed first since it is written inside
brackets.(From left to right or right to left. Refer operator
precedence table in Unit1Part1.pdf).
2. a*(b+c), or (m+n)*f executes multiplication operation secondly.
3. Finally division operation takes place.
COMMAND LINE ARGUMENTS
• Command line arguments are parameters that are supplied to the
application program at the time of invoking it for execution.
• Eg:- public static void main(String args[]).
• Eg: javac hello.java
java hello 123 abc 12.88
Then,
• args[0] contains 123
• args[1] contains abc
• args[2] contains 12.88
• Wrapper classes are used to convert the string to corresponding
simpler data type.
• ie, static int parseInt(String s)
• For example: int value= Integer.parseInt(“123”)
• Int total=args.length;
• Converts stirng 10 to integer data type.
Fibonacci series
class fib{
public static void main(String args[])
{
int n;
int a=0,b=1,c;
n=Integer.parseInt(args[0]);
for(int i=0;i<n;i++)
{
c=a+b;
System.out.println("The next number is "+c);
a=b;
b=c;
}
}
}
VARIABLES
• Variables refers to names of storage location
which is used to store data vales.
Eg: int i;
int i=3,j=5;
• Can take different values at different times during
execution of the program.
• Similar as identifiers.
• Assignment method
• readline() method.(Type casting)
Variables example- readLine()
import java.io.Console;
class exampleread
{
public static void main(String args[])
{
String str;
Console con = System.console();
if(con == null)
{
System.out.print("No console available");
return;
}
str = con.readLine("Enter your name: ");
con.printf("Here is your name: %sn", str);
}
}
VARIABLES(readLine() & Type casting
using wrapper classes)
import java.io.Console;
class examplereadline
{
try{
public static void main(String args[])
{
int age=0;
Console con = System.console();
age= Integer.parseInt(con.readLine("Enter your age") );
con.printf("Here is your age:%dn", age);
}
}
Catch{
}
}
Scope of variables
• Instance variables - Created when objects are instantiated and
takes different values for each object.
Eg: student s1=new student();
• Each object has its own copy of instance variables.
• Class variables - Available to all methods in a class.
• Local variables – Local to the method inside it is declared.
Scope of variables-Example
// Demonstrate block scope.
class Scope {
public static void main(String args[]) {
int x; // known to all code within main
x = 10;
if(x == 10) { // start new scope
int y = 20; // known only to this block
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
System.out.println("x is " + x);
}
}
CONSTANTS
• Constants refer to values that do not change
during the execution of the program.
• Java supports following types of constants.
• Integer constant. Eg:2021.
• Real/Floating point constant. Eg: 32.567.
• String constant. Eg: “Hello World”.
• Character constant. Eg: ‘a’.
• Final float pi=3.142;
• Apart from these types of constants java also
supports backslash character constants.
BACKSLASH CONSTANT PURPOSE
b Backspace
f Form feed
n New Line
r Carriage return
t Horizontal tab
 Backslash
DATA TYPES
Data types
Integer
char a = 'G';
int i=89;
byte b = 4;
short s = 56;
float f = 4.7333434f;
double d = 4.355453532d;
Data types- Example
class datatypes{
public static void main(String args[]){
char a = 'G';
int i=89;
byte b = 4;
short s = 3;
double d = 4.355453532d;
float f = 4.7333434f;
System.out.println("char: " + a);
System.out.println("integer: " + i);
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("float: " + f);
System.out.println("double: " + d);
}
}
ARRAYS
• An array is a group of contiguous/related data
items that share a common name.
• Syntax: Data type Arrayname[];
• Eg: int salary[9] or int[] salary;
• 3 steps to create the array.
• 1) Declare the array->Only a reference of array
created.
• 2) Create the memory allocation->
salary=new int[9];
• 3)Putting values to memory location.
Array representation:-
Int a=Arrayname.length; // To find length of the array.
One dimensional Arrays.
Array Literal
In a situation, where the size of the array and variables of
array are already known, array literals can be used.
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
Arrays as Parameters
 An entire array can be passed as a parameter to a
method
 Like any other object, the reference to the array is
passed, making the formal and actual parameters aliases
of each other
 Changing an array element within the method changes
the original
 An array element can be passed to a method as well, and
follows the parameter passing rules of that element's
type
class Testarray3{
//creating a method which receives an array as a parameter
static void min(int arr[]){
int min=arr[0];
for(int i=1;i<arr.length;i++)
if(min>arr[i])
min=arr[i];
System.out.println(min);
}
public static void main(String args[]){
int a[]={33,3,4,5};//declaring and initializing an array
min(a);//passing array to method
}}
Two Dimensional Arrays
int[][] intArray = new int[10][20]; //a 2D array
or matrix
Array-Assigning values can be done directly or
using loops.
public static void main(String args[])
{
// declaring and initializing 2D array
int arr[][] = { {2,7,9},{3,6,1},{7,4,2} };
// printing 2D array
for (int i=0; i< 3 ; i++)
{
for (int j=0; j < 3 ; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
Multi Dimensional Arrays
• int[][][] intArray = new int[10][20][10]; //a 3D
array .
• Known as Jagged Arrays.
• Strings- Character Array or String Objects.
STRINGS
• It is the most common part of many java
programs.
• String represent a sequence of characters.
• The simplest way to represent a sequence of
characters in java is by using a character array.
• Example:
char charArray[ ] = new char[4];
charArray[0] = ‘J’;
charArray[1] = ‘a’;
DECLARATION OF STRING
• Example:
STRING ARRAYS
• Array can be created and used that contain
strings.
• Above statement create following effects:
 In Java, strings are class objects and implemented
using two classes,namely, String and StringBuffer.
 A java string is an instantiated object of the String
class.
 Java string as compared to C strings, are more
reliable and predicable.
 A java string is not a character array and is not
NULL terminated.
STRING METHODS
 String class defines a number of methods that
allow us to accomplish a variety of string
manipulation tasks.
 Eg: 1) s2=s1.toLowercase;
2) s2=s1.toUppercase;
3) s1.compareTo(s2) //To sort array of
strings.
STRING BUFFER CLASS
 StringBuffer is a peer class of String.
 While String creates strings of fixed_length,
StringBuffer creates strings of flexible length that
can be modified in term of both length and content.
 We can insert characters and substrings in the
middle of a string to the end.

More Related Content

What's hot (20)

Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
Sowmya Jyothi
 
Yacc
YaccYacc
Yacc
BBDITM LUCKNOW
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
Vivek Singh Chandel
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab
MashaelQ
 
Code generation
Code generationCode generation
Code generation
Aparna Nayak
 
Monadic genetic kernels in Scala
Monadic genetic kernels in ScalaMonadic genetic kernels in Scala
Monadic genetic kernels in Scala
Patrick Nicolas
 
User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2
Shameer Ahmed Koya
 
Compiler Design Tutorial
Compiler Design Tutorial Compiler Design Tutorial
Compiler Design Tutorial
Sarit Chakraborty
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
BBDITM LUCKNOW
 
Programming in c by pkv
Programming in c by pkvProgramming in c by pkv
Programming in c by pkv
Pramod Vishwakarma
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
Dushmanta Nath
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 
Lexyacc
LexyaccLexyacc
Lexyacc
Hina Tahir
 
Functions
FunctionsFunctions
Functions
Online
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
Appili Vamsi Krishna
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
baran19901990
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Mansi Tyagi
 
Handout#10
Handout#10Handout#10
Handout#10
Sunita Milind Dol
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
Munsif Ullah
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
Salahaddin University-Erbil
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
Sowmya Jyothi
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
Vivek Singh Chandel
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab
MashaelQ
 
Monadic genetic kernels in Scala
Monadic genetic kernels in ScalaMonadic genetic kernels in Scala
Monadic genetic kernels in Scala
Patrick Nicolas
 
User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2
Shameer Ahmed Koya
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
Dushmanta Nath
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 
Functions
FunctionsFunctions
Functions
Online
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
Appili Vamsi Krishna
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
baran19901990
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Mansi Tyagi
 

Similar to Programming in java basics (20)

Java
JavaJava
Java
Ashen Disanayaka
 
JAVA programming language made easy.pptx
JAVA programming language made easy.pptxJAVA programming language made easy.pptx
JAVA programming language made easy.pptx
Sunila31
 
Java Basics
Java BasicsJava Basics
Java Basics
Brandon Black
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptxModule 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
rishiskj
 
Revision of introduction in java programming.pptx
Revision of introduction in java programming.pptxRevision of introduction in java programming.pptx
Revision of introduction in java programming.pptx
MutwakilElsadig
 
demo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.pptdemo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.ppt
FerdieBalang
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
Edureka!
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
kavitamittal18
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
DrPriyaChittibabu
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
shivanka2
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
Ashwani Kumar
 
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
JAVA CLASS PPT FOR ENGINEERING STUDENTS  BBBBBBBBBBBBBBBBBBBJAVA CLASS PPT FOR ENGINEERING STUDENTS  BBBBBBBBBBBBBBBBBBB
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
UNIT – 2 Features of java- (Shilpa R).pptx
UNIT – 2 Features of java- (Shilpa R).pptxUNIT – 2 Features of java- (Shilpa R).pptx
UNIT – 2 Features of java- (Shilpa R).pptx
shilpar780389
 
Object oriented programming1 Week 1.pptx
Object oriented programming1 Week 1.pptxObject oriented programming1 Week 1.pptx
Object oriented programming1 Week 1.pptx
MirHazarKhan1
 
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
GANESHBABUVelu
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
VijalJain3
 
Presentation on programming language on java.ppt
Presentation on programming language on java.pptPresentation on programming language on java.ppt
Presentation on programming language on java.ppt
HimambashaShaik
 
a basic java programming and data type.ppt
a basic java programming and data type.ppta basic java programming and data type.ppt
a basic java programming and data type.ppt
GevitaChinnaiah
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java
Ahmad Idrees
 
JAVA programming language made easy.pptx
JAVA programming language made easy.pptxJAVA programming language made easy.pptx
JAVA programming language made easy.pptx
Sunila31
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptxModule 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
rishiskj
 
Revision of introduction in java programming.pptx
Revision of introduction in java programming.pptxRevision of introduction in java programming.pptx
Revision of introduction in java programming.pptx
MutwakilElsadig
 
demo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.pptdemo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.ppt
FerdieBalang
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
Edureka!
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
shivanka2
 
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
JAVA CLASS PPT FOR ENGINEERING STUDENTS  BBBBBBBBBBBBBBBBBBBJAVA CLASS PPT FOR ENGINEERING STUDENTS  BBBBBBBBBBBBBBBBBBB
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
UNIT – 2 Features of java- (Shilpa R).pptx
UNIT – 2 Features of java- (Shilpa R).pptxUNIT – 2 Features of java- (Shilpa R).pptx
UNIT – 2 Features of java- (Shilpa R).pptx
shilpar780389
 
Object oriented programming1 Week 1.pptx
Object oriented programming1 Week 1.pptxObject oriented programming1 Week 1.pptx
Object oriented programming1 Week 1.pptx
MirHazarKhan1
 
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
GANESHBABUVelu
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
VijalJain3
 
Presentation on programming language on java.ppt
Presentation on programming language on java.pptPresentation on programming language on java.ppt
Presentation on programming language on java.ppt
HimambashaShaik
 
a basic java programming and data type.ppt
a basic java programming and data type.ppta basic java programming and data type.ppt
a basic java programming and data type.ppt
GevitaChinnaiah
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java
Ahmad Idrees
 
Ad

More from LovelitJose (6)

Professional ethics-Unit5
Professional ethics-Unit5Professional ethics-Unit5
Professional ethics-Unit5
LovelitJose
 
Professional ethics-Unit4
Professional ethics-Unit4Professional ethics-Unit4
Professional ethics-Unit4
LovelitJose
 
Professional ethics-Unit3
Professional ethics-Unit3Professional ethics-Unit3
Professional ethics-Unit3
LovelitJose
 
Professional Ethics Unit2
Professional Ethics Unit2Professional Ethics Unit2
Professional Ethics Unit2
LovelitJose
 
Professional ethics PPT unit 1
Professional ethics PPT unit 1Professional ethics PPT unit 1
Professional ethics PPT unit 1
LovelitJose
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
Professional ethics-Unit5
Professional ethics-Unit5Professional ethics-Unit5
Professional ethics-Unit5
LovelitJose
 
Professional ethics-Unit4
Professional ethics-Unit4Professional ethics-Unit4
Professional ethics-Unit4
LovelitJose
 
Professional ethics-Unit3
Professional ethics-Unit3Professional ethics-Unit3
Professional ethics-Unit3
LovelitJose
 
Professional Ethics Unit2
Professional Ethics Unit2Professional Ethics Unit2
Professional Ethics Unit2
LovelitJose
 
Professional ethics PPT unit 1
Professional ethics PPT unit 1Professional ethics PPT unit 1
Professional ethics PPT unit 1
LovelitJose
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
Ad

Recently uploaded (20)

David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior DevelopersDavid Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptxImpurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant ConversionStructural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
Ppt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineeringPpt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineering
ravindrabodke
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
ijfcstjournal
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)
pelumiadigun2006
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior DevelopersDavid Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptxImpurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant ConversionStructural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
Ppt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineeringPpt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineering
ravindrabodke
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
ijfcstjournal
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)
pelumiadigun2006
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 

Programming in java basics

  • 1. UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS Object Oriented Programming Paradigm - Basic Concepts of Object Oriented Programming – Benefits and Applications of OOP – Java Features – Java Program Structure – Command Line Arguments – Variables and Data Types – Operators – Expressions – Arrays - Control Statements – Selection – Iteration - Jump Statements
  • 3. Features of OBJECT ORIENTED PARADIGMS 1. Data 2. Objects 3. Data structures 4. Methods 5. Hidden data 6. Communication 7. Addition of new data 8. Bottom up approach
  • 4. Basic concepts of OBJECT ORIENTED PROGRAMMING I. OBJECTS & CLASSES II. DATA ABSTRACTION & ENCAPSULATION III. INHERITANCE IV. POLYMORPHISM V. DYNAMIC BINDING VI. MESSAGE COMMUNICATION
  • 5. I.OBJECTS & CLASSES • Object-Basic runtime entity • Class- Collection of objects of similar type
  • 6. II.DATA ABSTRACTION & ENCAPSULATION • Encapsulation–Wrapping up of data and methods into single unit(called class). • Data hiding –Insulation of data from direct access by program. • Data abstraction Representing essential features only.
  • 7. III.INHERITANCE • Objects of 1 class acquire the properties of objects of another class.
  • 8. IV.POLYMORPHISM • Ability to take more than one form
  • 9. V.DYNAMIC BINDING • Linking of procedure call to code. • Linking of method call to its corresponding method definition takes place only during runtime.
  • 10. VI.MESSAGE COMMUNICATION There are 3 steps. • Class creation • Object creation • Establish communication
  • 11. BENEFITS OF OOP • Inheritance • Time saving and high productivity. • Data hiding • Multiple objects • Mapping of objects • Easy to upgrade • Communication through messages • Can manage software complexity easily
  • 12. APPLICATIONS OF OOP • Real time systems. • Simulation & Modelling. • Object oriented DB • AI • Neural networks
  • 13. JAVA FEATURES Introduction:- • Java is an object oriented language developed by Sun Microsystems. • Original name was OAK . Later renamed to JAVA. • Platform neutral language(platform independent.)(Java Virtual Machine). • Java is both compiled and interpreted language.
  • 16. JAVA FEATURES 1. Java is both a compiled and an interpreted language. 2. Platform independent and portable-Write Once Run Anywhere. 3. Object Oriented. 4. Robust and Secure. 5. Distributed. 6. Automatic Memory Management. 7. Dynamic binding 8. High performance. 9. Multithreading.
  • 17. Some features in C/C++ are eliminated in java. They are listed below. • No Pointers • No Preprocessor Directives • No Multiple Inheritance in Classes • No operator overloading. • No global variables.
  • 18. Simple Java Program class Welcome { public static void main(String args[]) { System.out.println(“Welcome to Java world “); } }
  • 20. Simple Java Program /* * To change this template, choose Tools | Templates and open the template in the editor. */ package college; import java.io.IOException; import java.io.PrintWriter; import java.sql; class sum { public static void main(String args[]) { int i=10,j=25; k=i+j; System.out.println(“THE SUM IS “+k); } }
  • 21. Download JDK and install in Windows10- Use the following YouTube link. https://video.search.yahoo.com/search/video?fr =mcafee&ei=UTF- 8&p=download+jdk&type=E211US1045G0#id=4 &vid=3aa873b105c9f56e6a73b619da6222ba&a ction=click Compilation code- javac filename.java Execution code- java filename
  • 22. JAVA TOKENS • In Java every statement is made up of smallest individual elements called tokens. Tokens in java include the following:- 1. Keywords 2. Identifiers 3. Literals 4. Operators 5. Separators
  • 23. 1. Keywords • Keywords cannot be redefined by a programmer • Keywords cannot be used as identifier name. Eg: Abstract, Assert, Boolean, Break, Byte, Case. • Java language has reserved 50 words as keywords.
  • 24. 2. Identifiers • Programmer designed tokens. Used for naming variables, methods, classes etc. • An identifier can contain alphanumeric characters, but it should begin with an alphabet. • Identifiers are case sensitive. • Underscore and dollar sign are two special character allowed. • Eg:A23C • Bob@gmail - Wrong
  • 25. 3. Literals • LITERAL is a program element that represents an exact (fixed) value of a certain type. Java language supports the following type of literals. 1. Integer Literals. (Ex. 123) 2. Floating point Literals. (Ex. 88.223) 3. Character Literals. (Ex. ‘.’ , ‘a’ , ‘n’) 4. String Literals. (Ex. “Hello World”) 5. Boolean Literals. (Ex. True,false)
  • 26. 4. Separators • Symbols used to indicate where group of code are divided and arranged. • () • {} • [] • ; • , • .
  • 27. 5. Operators • Tells the computer to perform certain mathematical or logical manipulations on data and variables. Different types are:- 1. Arithmetic Operators. 2. Unary Operators. 3. Assignment Operators. 4. Equality and Relational Operators. 5. Logical and Conditional Operators. 6. Bitwise and Bit Shift Operators. 7. Special operators
  • 28. 1. Arithmetic Operators Eg: a+b a-b a*b a/b a%b 2. Unary Operators Eg: +b -b ++b --b
  • 29. 3. Assignment Operators Eg:a=b+c; a=a+1; and a+=1; same meaning. a+=1 is shorthand assignment operator. 4. Equality and Relational Operators Eg: a==b; a!=b; • Value of relational expression is either true or false. • It is used in decision statements. Eg: a<b; a>b; a<=b; a>=b;
  • 30. 5. Logical and Conditional Operators. • Used to form compound conditions by combining 2 or more relations. && -logical AND || -logical OR. ! – logical compliment. Eg: a>b && x==10; ?: - Conditional ternary opeartor(shorthand for if-then-else statement). Eg: x=(a>b)?a:b;
  • 31. 6. Bitwise and Bit Shift Operators ~ Unary bitwise complement << Signed left shift >> Signed right sift >>> Unsigned right shift & Bitwise AND ^ Bitwise exclusive OR | Bitwise inclusive OR
  • 32. 7. Special Operators 1. Instanceof operator Eg: person instanceof student. • Checks whether object person belongs to class student or not. • Returns either true or false 2. Dot operator /member selection operator • Used to access variables and methods of a class object. Eg: Person1.age; Person1.salary(); Where person1 is an object . Age is a variable Salary() is a method.
  • 33. Example:Operators • public class StringLowerUpperExample{ • public static void main(String args[]){ • String s1="JAVATPOINT HELLO stRIng"; • String s1lower=s1.toLowerCase(); • System.out.println(s1lower); • String s2="hello string"; • String s1upper=s2.toUpperCase(); • System.out.println(s1upper); • }}
  • 34. Operator Precedence Table Operator Precedence Operators Precedence postfix expr++ expr-- unary ++expr --expr +expr -expr ~ ! multiplicative * / % additive + - shift << >> >>> relational < > <= >= instanceof equality == != bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | logical AND && logical OR || ternary ? : assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
  • 35. EXPRESSIONS • Java expressions are used to implement some computation. • Syntax: Variable=Exprerssion; • Simple Expression : x=a+b; • Complicated expression : a*(b+c)/(m+n)*f; In this example, 1. (b+c) or (m+n) executed first since it is written inside brackets.(From left to right or right to left. Refer operator precedence table in Unit1Part1.pdf). 2. a*(b+c), or (m+n)*f executes multiplication operation secondly. 3. Finally division operation takes place.
  • 36. COMMAND LINE ARGUMENTS • Command line arguments are parameters that are supplied to the application program at the time of invoking it for execution. • Eg:- public static void main(String args[]). • Eg: javac hello.java java hello 123 abc 12.88 Then, • args[0] contains 123 • args[1] contains abc • args[2] contains 12.88 • Wrapper classes are used to convert the string to corresponding simpler data type. • ie, static int parseInt(String s) • For example: int value= Integer.parseInt(“123”) • Int total=args.length; • Converts stirng 10 to integer data type.
  • 37. Fibonacci series class fib{ public static void main(String args[]) { int n; int a=0,b=1,c; n=Integer.parseInt(args[0]); for(int i=0;i<n;i++) { c=a+b; System.out.println("The next number is "+c); a=b; b=c; } } }
  • 38. VARIABLES • Variables refers to names of storage location which is used to store data vales. Eg: int i; int i=3,j=5; • Can take different values at different times during execution of the program. • Similar as identifiers. • Assignment method • readline() method.(Type casting)
  • 39. Variables example- readLine() import java.io.Console; class exampleread { public static void main(String args[]) { String str; Console con = System.console(); if(con == null) { System.out.print("No console available"); return; } str = con.readLine("Enter your name: "); con.printf("Here is your name: %sn", str); } }
  • 40. VARIABLES(readLine() & Type casting using wrapper classes) import java.io.Console; class examplereadline { try{ public static void main(String args[]) { int age=0; Console con = System.console(); age= Integer.parseInt(con.readLine("Enter your age") ); con.printf("Here is your age:%dn", age); } } Catch{ } }
  • 41. Scope of variables • Instance variables - Created when objects are instantiated and takes different values for each object. Eg: student s1=new student(); • Each object has its own copy of instance variables. • Class variables - Available to all methods in a class. • Local variables – Local to the method inside it is declared.
  • 42. Scope of variables-Example // Demonstrate block scope. class Scope { public static void main(String args[]) { int x; // known to all code within main x = 10; if(x == 10) { // start new scope int y = 20; // known only to this block // x and y both known here. System.out.println("x and y: " + x + " " + y); x = y * 2; } // y = 100; // Error! y not known here // x is still known here. System.out.println("x is " + x); } }
  • 43. CONSTANTS • Constants refer to values that do not change during the execution of the program. • Java supports following types of constants.
  • 44. • Integer constant. Eg:2021. • Real/Floating point constant. Eg: 32.567. • String constant. Eg: “Hello World”. • Character constant. Eg: ‘a’. • Final float pi=3.142; • Apart from these types of constants java also supports backslash character constants. BACKSLASH CONSTANT PURPOSE b Backspace f Form feed n New Line r Carriage return t Horizontal tab Backslash
  • 46. Data types Integer char a = 'G'; int i=89; byte b = 4; short s = 56; float f = 4.7333434f; double d = 4.355453532d;
  • 47. Data types- Example class datatypes{ public static void main(String args[]){ char a = 'G'; int i=89; byte b = 4; short s = 3; double d = 4.355453532d; float f = 4.7333434f; System.out.println("char: " + a); System.out.println("integer: " + i); System.out.println("byte: " + b); System.out.println("short: " + s); System.out.println("float: " + f); System.out.println("double: " + d); } }
  • 48. ARRAYS • An array is a group of contiguous/related data items that share a common name. • Syntax: Data type Arrayname[]; • Eg: int salary[9] or int[] salary; • 3 steps to create the array. • 1) Declare the array->Only a reference of array created. • 2) Create the memory allocation-> salary=new int[9]; • 3)Putting values to memory location.
  • 49. Array representation:- Int a=Arrayname.length; // To find length of the array. One dimensional Arrays. Array Literal In a situation, where the size of the array and variables of array are already known, array literals can be used. int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
  • 50. class Testarray{ public static void main(String args[]){ int a[]=new int[5];//declaration and instantiation a[0]=10;//initialization a[1]=20; a[2]=70; a[3]=40; a[4]=50; //traversing array for(int i=0;i<a.length;i++)//length is the property of array System.out.println(a[i]); }}
  • 51. Arrays as Parameters  An entire array can be passed as a parameter to a method  Like any other object, the reference to the array is passed, making the formal and actual parameters aliases of each other  Changing an array element within the method changes the original  An array element can be passed to a method as well, and follows the parameter passing rules of that element's type
  • 52. class Testarray3{ //creating a method which receives an array as a parameter static void min(int arr[]){ int min=arr[0]; for(int i=1;i<arr.length;i++) if(min>arr[i]) min=arr[i]; System.out.println(min); } public static void main(String args[]){ int a[]={33,3,4,5};//declaring and initializing an array min(a);//passing array to method }}
  • 53. Two Dimensional Arrays int[][] intArray = new int[10][20]; //a 2D array or matrix
  • 54. Array-Assigning values can be done directly or using loops. public static void main(String args[]) { // declaring and initializing 2D array int arr[][] = { {2,7,9},{3,6,1},{7,4,2} }; // printing 2D array for (int i=0; i< 3 ; i++) { for (int j=0; j < 3 ; j++) System.out.print(arr[i][j] + " "); System.out.println(); } }
  • 55. Multi Dimensional Arrays • int[][][] intArray = new int[10][20][10]; //a 3D array . • Known as Jagged Arrays. • Strings- Character Array or String Objects.
  • 56. STRINGS • It is the most common part of many java programs. • String represent a sequence of characters. • The simplest way to represent a sequence of characters in java is by using a character array. • Example: char charArray[ ] = new char[4]; charArray[0] = ‘J’; charArray[1] = ‘a’;
  • 58. STRING ARRAYS • Array can be created and used that contain strings. • Above statement create following effects:
  • 59.  In Java, strings are class objects and implemented using two classes,namely, String and StringBuffer.  A java string is an instantiated object of the String class.  Java string as compared to C strings, are more reliable and predicable.  A java string is not a character array and is not NULL terminated.
  • 60. STRING METHODS  String class defines a number of methods that allow us to accomplish a variety of string manipulation tasks.  Eg: 1) s2=s1.toLowercase; 2) s2=s1.toUppercase; 3) s1.compareTo(s2) //To sort array of strings.
  • 61. STRING BUFFER CLASS  StringBuffer is a peer class of String.  While String creates strings of fixed_length, StringBuffer creates strings of flexible length that can be modified in term of both length and content.  We can insert characters and substrings in the middle of a string to the end.