SlideShare a Scribd company logo
Object Oriented Programming Using Java
BASIC ELEMENTS OF JAVA PROGRAMMING
School of Computer Engineering
KIIT Deemed to be University
Dr.Sunil Kumar Gouda
Asst. Professor
01/25/2025 Java Programming, Sunil Kumar Gouda
First JAVA Program
Program: To print a message Welcome to the JAVA World
class First
{
public static void main (String args[])
{
System.out.println("Welcome to the JAVA world");
}
}
Output:
Welcome to the JAVA world
01/25/2025 Java Programming, Sunil Kumar Gouda
How to compile and execute a Java Program
 Command to Compile a java program on the command line:
javac file_name.java
Following command is used to compile the First.java program
javac First.java
 Syntax to Run/Execute a java program on the command line:
java file_name
Following command is used to run/ execute the First.java program
java First
01/25/2025 Java Programming, Sunil Kumar Gouda
Explanation of the above program (First.java)
 class First
In a purely object oriented programming system, all the codes remain
inside a class definition. Therefore, the program begins with a class
definition. Here class is a keyword used to define a class & First is the
name of our class.
{
Here this opening brace indicates the beginning of the class definition
block.
 public static void main(String args[])
All Java applications begin execution from main () function. (This is just
like C/C++.)

01/25/2025 Java Programming, Sunil Kumar Gouda
Explanation of the above program (First.java)
The public keyword is an access specifier, which allows the programmer
to control the visibility of class members. When a class member is
preceded by public, then that member may be accessed by code outside
the class in which it is declared.
The keyword static allows main () to be called without having to
instantiate a particular instance of the class.
The keyword void simply tells the compiler that main () does not return a
value. As stated, main () is the method called when a Java application
begins. Keep in mind that Java is case-sensitive. Thus, Main is different
from main.
String args[] declares a parameter named args, which is an array of
instances of the class String. This args parameter is essential when we
enter some input through the command line while executing a program.
01/25/2025 Java Programming, Sunil Kumar Gouda
Explanation of the above program (First.java)
 {
Here this opening brace indicates the beginning of main method
definition block.
 System.out.println("Welcome to the JAVA world ");
This line outputs the string “Welcome to the JAVA world” followed by a
new line on the screen.Output is actually accomplished by the built-in
println() method. In this case, println() displays the string which is passed to
it.
 }
Here this closing brace indicates the end of main method definition
block.
 }
Here this closing brace indicates the end of the class definition block.
01/25/2025 Java Programming, Sunil Kumar Gouda
Java is a Strongly Typed Language
 It is important to state at the outset that Java is a strongly typed
language. Main reasons are-
 Every variable has a type, every expression has a type, and every type is
strictly defined.
 All assignments, whether explicit or via parameter passing in method
calls, are checked for type compatibility.
01/25/2025 Java Programming, Sunil Kumar Gouda
Data Types
 Data types specify the different size and value that can be stored in a
variable. There are two types of data types in Java:
 Primitive data types: Java defines eight primitive types of data. The
primitive data types (or simple data types) include boolean, char, byte,
short, int, long, float and double which are put in four distinct groups (as
shown in the next slide).
 Non-primitive data types: These are user defined datatypes. The non-
primitive data types include class, interface, and array etc.
01/25/2025 Java Programming, Sunil Kumar Gouda
Primitive Data Types
Data Type Default Value Size Range
Boolean boolean false 1 bit 0 or 1
Character char 'u0000' 2 byte -32768 to 32767 (-215
to 215
-1)
Integer
byte 0 1 byte -128 to 127 (-27
to 27
-1)
short 0 2 byte -32768 to 32767 (-215
to 215
-1)
int 0 4 byte -2,147,483,648 to 2,147,483,647 (231
to 231
-1)
long 0L 8 byte -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807 (-263
to
263
-1)
Float/
Real
float 0.0f 4 byte -231
to 231
-1
double 0.0d 8 byte -263
to 263
-1
01/25/2025 Java Programming, Sunil Kumar Gouda
Primitive Data Types Cont..
In general, if n bits are required for a data type, then its range is
−2𝑛−1
¿2𝑛−1
−1
01/25/2025 Java Programming, Sunil Kumar Gouda
Java Characters Take 2 Bytes of Memory !!
 It is because java uses Unicode system, not ASCII (American Standard
Code for Information Interchange) code system.
 Unicode system represents a character set which can be used to
represent all languages available world. u0000 is the lowest range of
Unicode system.
01/25/2025 Java Programming, Sunil Kumar Gouda
Java tokens
 The smallest unit of code appearing in a program which is recognized by
the compiler is called as token.
 A token in a java program are of following types:
Identifiers
Literals/ Constant
Keywords
Operators
Comment
Special characters
01/25/2025 Java Programming, Sunil Kumar Gouda
Identifiers
 These are names assigned to any variable, method, classe, package or
interface. Following are the rules to construct an identifier:
An identifier contains alphabets, digits, underscore or dollar ($) only.
The first character in an identifier must be an alphabet or underscore(_)
or dollar($).
Blank spaces are not allowed within an identifier.
Keywords can’t be identifiers.
Identifier name are case sensitive.
There is no limit on the length of a Java identifier.
 Some valid identifiers are x, a1, $p, my_var etc.
01/25/2025 Java Programming, Sunil Kumar Gouda
Literals
 Literals in Java are a sequence of characters (digits, letters, and other
characters) that represent constant values to be stored in variables. Java
offers following types of literals:
Integer literals: These are numeric values without a decimal point.
Examples of integer literals are 1, -34, 1083 (in decimal) etc. Two other
bases that can be used in integer literals are octal (base eight) and
hexadecimal (base 16). Octal values are denoted in Java by a leading 0
and hexadecimal hexadecimal values are denoted by leading 0x or 0X.
Character literals: Any single character enclosed in a single quote (‘ ’) is
termed as a character literal. Examples of character literals are ‘A’, ‘5’, ‘ ’,
‘.’ etc.
01/25/2025 Java Programming, Sunil Kumar Gouda
Literals Cont..
Floating literals: These are numeric values with a decimal point and
fractional component. Examples of floating literals are 1.97, -34.07,
2009.06 (standard notation), 1.56e+78, 1.09e-9 (scientific notation) etc.
Floating-point literals in Java default to double precision. To specify a
float literal, we must append an F or f to the constant. We can also explicitly
specify a double literal by appending a D or d. Doing so is, of course,
redundant.
String literals: One or more character enclosed in a double quote (“ ”) is
termed as a string literal. Examples of string literals are “Java
Programming”, “5” etc.
Boolean literals: These are literals which acquire Boolean values true or
false.
Null literals: The null in Java is a literal of the null type. It cannot be cast to
a primitive type such as int. float etc, but can be cast to a reference type.
Also, null does not have the value 0 necessarily.
01/25/2025 Java Programming, Sunil Kumar Gouda
Keywords
 Keywords or Reserved words are the words that represent some
predefined actions.
 Keywords are therefore not allowed to use as a variable names or
objects.
 There are 49 keywords in java.
 Examples: abstract, boolean, break, byte, case, catch, char, class,
continue, default, do, double, else, enum, extends, final, finally, float, for,
if, implements, import, instanceof, int, interface, long, native, new, null,
package, private, protected, public, return, short, static, strictfp, super,
switch, synchronized, this, throw, throws, transient, try, void, volatile,
while.
01/25/2025 Java Programming, Sunil Kumar Gouda
Operators
 Operator in java is a symbol that is used to perform operations on one or
more operands.
 Depending upon number of operands, operators are classified as-
Type of operator Number of operands Example
Unary 1 -, ++, -- etc
Binary 2 +, -, /, *, % etc
Ternary 3 ?:
01/25/2025 Java Programming, Sunil Kumar Gouda
Types of Operators
 Various types of operators in java are
 Arithmetic Operator
Relational Operator
Logical Operator
Increment & decrement Operator
Assignment Operator
Ternary Operator
Bitwise and Shift Operator
instanceof operator
arrow operator
01/25/2025 Java Programming, Sunil Kumar Gouda
Arithmetic Operator
Arithmetic operator Yields Example
Addition (+) addition result 5+3=8
Subtraction () difference result 5-3=2
Multiplication (*) product result 5*3=15
Division (/) division result 5/3=1
Modular Division (%) remainder result 5%3=2
01/25/2025 Java Programming, Sunil Kumar Gouda
Relational Operator
Relational
Operator
Meaning Example
> Strictly greater than 2>5 yields false
>= Greater than or equal to 5>=4 yields true
< Strictly less than 5<100 yields true
<= Smaller than or equal to 5<=5 yields true
== Double equal to 6==7 yields false
!= Not equal to 6!=7 yields true
01/25/2025 Java Programming, Sunil Kumar Gouda
Logical Operator
Operator Meaning
&&
Logical AND, a && b is true when both a and b
are true, false otherwise.
||
Logical OR, a || b is false when both a and b
are false, true otherwise.
!
Logical NOT, !a is true if a is false and vice-
versa.
01/25/2025 Java Programming, Sunil Kumar Gouda
Increment/ Decrement Operator
Expression Meaning
y=x++ Value of x is assigned to y before it is
incremented
Y=++x Value of x is incremented first and then
assigned to y
01/25/2025 Java Programming, Sunil Kumar Gouda
Assignment Operator
 Assignment operators are used in Java to assign values to variables.
 For example, int age=5;
The assignment operator assigns the value on its right to the variable on its
left. Here, 5 is assigned to the variable age using = operator.
 Shorthand assignment operator: Operators like +=, -=, *=, /= etc are
known as shorthand assignment operator.
x+=1 is equivalent to x=x+1
y/=9 is equivalent to y=y/9 etc.
01/25/2025 Java Programming, Sunil Kumar Gouda
Conditional Operator
 This operator consists of three operands and is used to evaluate boolean
expressions. The goal of this operator is to decide; which value should be assigned
to the variable. The operator is written as:
variable x = (expression)? value if true:value if false
Example:
public class Test{
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println("Value of b is: " + b);
b = (a == 10) ? 20: 30;
System.out.println(“Value of b is: " + b);
}
}
Output:
Value of b is: 30
Value of b is: 20
01/25/2025 Java Programming, Sunil Kumar Gouda
Comment Line
 Comments are statements that are not executed by the compiler and
interpreter.
 The comments can be used to provide information or explanation about
the variable, method, class or any statement.
 These can also be used to hide program code for specific time.
 In Java there are three types of comments:
 Single line comments (//single line of text)
 Multi line comments (/*multiple lines of text /*)
 Documentation comments (/** documentation text*/)
01/25/2025 Java Programming, Sunil Kumar Gouda
Type Casting
 The method of converting one primitive data type into another data type
is called type casting.
 There are two types of typecasting:
Implicit type casting or Widening casting
Explicit type casting or Narrowing casting
01/25/2025 Java Programming, Sunil Kumar Gouda
Implicit type casting or Widening casting
 Implicit type casting or widening casting:This is the process of
converting a smaller type to a larger type. When one type of data is
assigned to another type of variable, an automatic type conversion will
take place if the following two conditions are met:
The two types are compatible.
The destination type is larger than the source type.
 For widening conversions, the numeric types, including integer and
floating point types, are compatible with each other. However, there are
no automatic conversions from the numeric types to char or boolean.
Also, char and boolean are not compatible with each other.
01/25/2025 Java Programming, Sunil Kumar Gouda
Implicit type casting or Widening casting Cont..
Example:
public class ImplicitTC
{
public static void main(String[] args)
{
int myInt = 9;
double myDouble = myInt;
System.out.println(myInt);
System.out.println(myDouble);
}
}
Output:
9
9.0
01/25/2025 Java Programming, Sunil Kumar Gouda
Explicit type casting or Narrowing casting
 This type casting is done manually by placing the type in parentheses in
front of the value. This is done manually by the programmer. Hence it is
also known as explicit type casting.
 For example, the following fragment casts an int to a byte. If the
integer’s value is larger than the range of a byte, it will be reduced
modulo to (the remainder of an integer division by the) byte’s range.
int a;
byte b;
// …
b = (byte) a;
 A different type of conversion will occur when a floating-point value is
assigned to an integer type: truncation.
01/25/2025 Java Programming, Sunil Kumar Gouda
Explicit type casting or Narrowing casting Cont..
Example:
public class ExplicitTC
{
public static void main(String[] args)
{
double myDouble = 9.78;
int myInt = (int) myDouble;
System.out.println(myDouble);
System.out.println(myInt);
}
}
Output:
9.78
9
01/25/2025 Java Programming, Sunil Kumar Gouda
Automatic Type Promotion in Expressions
 Consider following code:
byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c;
 The result of the intermediate term a*b easily exceeds the range of
either of its byte operands.
 To handle this kind of problem, Java automatically promotes each byte,
short, or char operand to int when evaluating an expression.
 This means that the sub expression a*b is performed using integers—
not bytes. Thus, 2,000, the result of the intermediate expression, 50*40,
is legal even though a and b are both specified as type byte.
01/25/2025 Java Programming, Sunil Kumar Gouda
Automatic Type Promotion in Expressions Contd..
 Consider following code:
byte b = 50;
b = b * 2; // Error! Cannot assign an int to a byte without cast !
 The code is attempting to store 50*2, a perfectly valid byte value, back
into a byte variable.
 However, because the operands were automatically promoted to int
when the expression was evaluated, the result has also been promoted
to int.
 Thus, the result of the expression is now of type int, which cannot be
assigned to a byte without the use of type casting.
01/25/2025 Java Programming, Sunil Kumar Gouda
Type Promotion Rules
 Java defines several type promotion rules that apply to expressions.
First, all byte, short, and char values are promoted to int.
Then, if one operand is a long, the whole expression is promoted to
long.
If one operand is a float, the entire expression is promoted to float.
If any of the operands are double, the result is double.
01/25/2025 Java Programming, Sunil Kumar Gouda
Array
 An array is a group of like-typed variables that are referred to by a
common name.
 Arrays of any type can be created and may have one or more
dimensions.
 A specific element in an array is accessed by its index. Arrays offer a
convenient means of grouping related information.
 Different types of arrays are:
One dimensional (1D) array
Multi dimensional (nD) array
01/25/2025 Java Programming, Sunil Kumar Gouda
One Dimensional (1-D) Array
 The general form of a one-dimensional array declaration is:
data_type var-name[ ];
 For example, the following declares an array named month_days with
the type “array of int”:
int month_days[];
 To allocate memory for arrays, new operator is applied to one-
dimensional arrays as follows:
array-var = new type [size];
 Therefore, obtaining an array is a two-step process.
First, we must declare a variable of the desired array type.
Second, we must allocate the memory that will hold the array, using new,
and assign it to the array variable.
 Thus, in Java all arrays are dynamically allocated.
01/25/2025 Java Programming, Sunil Kumar Gouda
Multi Dimensional (n-D) Array
 In Java, multidimensional arrays are implemented as array of arrays.
 To declare a multidimensional array variable, specify each additional
index using another set of square brackets.
 For example, the following declares a two dimensional array variable
called twoD:
int twoD[][] = new int[4][5];
01/25/2025 Java Programming, Sunil Kumar Gouda
Alternative Array Declaration Syntax
 There is a second form that may be used to declare an array:
type[] var-name;
 Following two declarations are equivalent:
int al[] = new int[3];
int[] a2 = new int[3];
 The following declarations are also equivalent:
char twod1[][] = new char[3][4];
char[][] twod2 = new char[3][4];
01/25/2025 Java Programming, Sunil Kumar Gouda
END OF THE CHAPTER
Ad

More Related Content

Similar to Ch3_Elements of JAVA Programming.pptx ka (20)

CSL101_Ch1.ppt Computer Science
CSL101_Ch1.ppt          Computer ScienceCSL101_Ch1.ppt          Computer Science
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
Java Unit-1.1 chunri kyu shuru kar rh koi si je di of du th n high ch ka dh h...
Java Unit-1.1 chunri kyu shuru kar rh koi si je di of du th n high ch ka dh h...Java Unit-1.1 chunri kyu shuru kar rh koi si je di of du th n high ch ka dh h...
Java Unit-1.1 chunri kyu shuru kar rh koi si je di of du th n high ch ka dh h...
gkgupta1115
 
Mobile computing for Bsc Computer Science
Mobile computing for Bsc Computer ScienceMobile computing for Bsc Computer Science
Mobile computing for Bsc Computer Science
ReshmiGopinath4
 
Programming with Java - Essentials to program
Programming with Java - Essentials to programProgramming with Java - Essentials to program
Programming with Java - Essentials to program
leaderHilali1
 
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
 
Chapter 2 java
Chapter 2 javaChapter 2 java
Chapter 2 java
Ahmad sohail Kakar
 
Java Simplified: Understanding Programming Basics
Java Simplified: Understanding Programming BasicsJava Simplified: Understanding Programming Basics
Java Simplified: Understanding Programming Basics
Akshaj Vadakkath Joshy
 
2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment
CarlosPineda729332
 
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptxModule 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
rishiskj
 
Overview of java Language-3.pdf
Overview of java Language-3.pdfOverview of java Language-3.pdf
Overview of java Language-3.pdf
kumari36
 
DAY_1.1.pptx
DAY_1.1.pptxDAY_1.1.pptx
DAY_1.1.pptx
ishasharma835109
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
Dr. SURBHI SAROHA
 
OOP Poster Presentation
OOP Poster PresentationOOP Poster Presentation
OOP Poster Presentation
Md Mofijul Haque
 
JAVA Basics Presentation for introduction to JAVA programming languauge
JAVA Basics Presentation for introduction to JAVA programming languaugeJAVA Basics Presentation for introduction to JAVA programming languauge
JAVA Basics Presentation for introduction to JAVA programming languauge
lakshyajain0740
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
Madishetty Prathibha
 
Java_Roadmap.pptx
Java_Roadmap.pptxJava_Roadmap.pptx
Java_Roadmap.pptx
ssuser814cf2
 
Full CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java languageFull CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java language
ssuser2963071
 
c#.pptx
c#.pptxc#.pptx
c#.pptx
JoselitoJMebolos
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
Marwa Ali Eissa
 
java intro.pptx.pdf
java intro.pptx.pdfjava intro.pptx.pdf
java intro.pptx.pdf
TekobashiCarlo
 
CSL101_Ch1.ppt Computer Science
CSL101_Ch1.ppt          Computer ScienceCSL101_Ch1.ppt          Computer Science
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
Java Unit-1.1 chunri kyu shuru kar rh koi si je di of du th n high ch ka dh h...
Java Unit-1.1 chunri kyu shuru kar rh koi si je di of du th n high ch ka dh h...Java Unit-1.1 chunri kyu shuru kar rh koi si je di of du th n high ch ka dh h...
Java Unit-1.1 chunri kyu shuru kar rh koi si je di of du th n high ch ka dh h...
gkgupta1115
 
Mobile computing for Bsc Computer Science
Mobile computing for Bsc Computer ScienceMobile computing for Bsc Computer Science
Mobile computing for Bsc Computer Science
ReshmiGopinath4
 
Programming with Java - Essentials to program
Programming with Java - Essentials to programProgramming with Java - Essentials to program
Programming with Java - Essentials to program
leaderHilali1
 
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
 
Java Simplified: Understanding Programming Basics
Java Simplified: Understanding Programming BasicsJava Simplified: Understanding Programming Basics
Java Simplified: Understanding Programming Basics
Akshaj Vadakkath Joshy
 
2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment
CarlosPineda729332
 
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptxModule 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
rishiskj
 
Overview of java Language-3.pdf
Overview of java Language-3.pdfOverview of java Language-3.pdf
Overview of java Language-3.pdf
kumari36
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
Dr. SURBHI SAROHA
 
JAVA Basics Presentation for introduction to JAVA programming languauge
JAVA Basics Presentation for introduction to JAVA programming languaugeJAVA Basics Presentation for introduction to JAVA programming languauge
JAVA Basics Presentation for introduction to JAVA programming languauge
lakshyajain0740
 
Full CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java languageFull CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java language
ssuser2963071
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
Marwa Ali Eissa
 

Recently uploaded (20)

美国学位证(纽约州立大学德里分校毕业证明)SUNYD文凭证书复刻
美国学位证(纽约州立大学德里分校毕业证明)SUNYD文凭证书复刻美国学位证(纽约州立大学德里分校毕业证明)SUNYD文凭证书复刻
美国学位证(纽约州立大学德里分校毕业证明)SUNYD文凭证书复刻
Taqyea
 
Introduction on Speaking skills Power Point
Introduction on Speaking skills Power PointIntroduction on Speaking skills Power Point
Introduction on Speaking skills Power Point
helenswarna
 
Latest Questions & Answers | Prepare for H3C GB0-961 Certification
Latest Questions & Answers | Prepare for H3C GB0-961 CertificationLatest Questions & Answers | Prepare for H3C GB0-961 Certification
Latest Questions & Answers | Prepare for H3C GB0-961 Certification
NWEXAM
 
RightShip-Inspection-Maritime-Safety-Simplified.pptx
RightShip-Inspection-Maritime-Safety-Simplified.pptxRightShip-Inspection-Maritime-Safety-Simplified.pptx
RightShip-Inspection-Maritime-Safety-Simplified.pptx
ultronmeg
 
Top MBA Specializations List at ISMS Pune.pdf
Top MBA Specializations List at ISMS Pune.pdfTop MBA Specializations List at ISMS Pune.pdf
Top MBA Specializations List at ISMS Pune.pdf
ISMS PUne
 
Handling Exceptions and waits in selenium.pptx
Handling Exceptions and waits in selenium.pptxHandling Exceptions and waits in selenium.pptx
Handling Exceptions and waits in selenium.pptx
Madhuri Lonikar
 
sorcesofdrugs-160228074 56 4246643544 (3).ppt
sorcesofdrugs-160228074 56 4246643544 (3).pptsorcesofdrugs-160228074 56 4246643544 (3).ppt
sorcesofdrugs-160228074 56 4246643544 (3).ppt
IndalSatnami
 
History of Entomology and current updates of entomology.pptx
History of Entomology and current updates of entomology.pptxHistory of Entomology and current updates of entomology.pptx
History of Entomology and current updates of entomology.pptx
Neelesh Raipuria
 
Huckel_MO_Theory_Colorful_Presentation (1).pptx
Huckel_MO_Theory_Colorful_Presentation (1).pptxHuckel_MO_Theory_Colorful_Presentation (1).pptx
Huckel_MO_Theory_Colorful_Presentation (1).pptx
study2022bsc
 
2. Basic Legal terminologies in law for paralegal officers .docx
2. Basic Legal terminologies in law for paralegal officers .docx2. Basic Legal terminologies in law for paralegal officers .docx
2. Basic Legal terminologies in law for paralegal officers .docx
JosiahOngai1
 
Organic Reactions Overviewwwweeeeeeee.pptx
Organic Reactions Overviewwwweeeeeeee.pptxOrganic Reactions Overviewwwweeeeeeee.pptx
Organic Reactions Overviewwwweeeeeeee.pptx
JayPatel845511
 
Top Business Schools in Delhi For Quality Education
Top Business Schools in Delhi For Quality EducationTop Business Schools in Delhi For Quality Education
Top Business Schools in Delhi For Quality Education
top10privatecolleges
 
Research Project csi1 - This presentation compares popular web browsers such ...
Research Project csi1 - This presentation compares popular web browsers such ...Research Project csi1 - This presentation compares popular web browsers such ...
Research Project csi1 - This presentation compares popular web browsers such ...
bomisung0207
 
remakingyourselfpresentation-250430095415-6476ade1.pptx
remakingyourselfpresentation-250430095415-6476ade1.pptxremakingyourselfpresentation-250430095415-6476ade1.pptx
remakingyourselfpresentation-250430095415-6476ade1.pptx
lakhmanpindariya9176
 
Huckel_Molecular orbital _Theory_8_Slides.pptx
Huckel_Molecular orbital _Theory_8_Slides.pptxHuckel_Molecular orbital _Theory_8_Slides.pptx
Huckel_Molecular orbital _Theory_8_Slides.pptx
study2022bsc
 
Stakeholders Management GT 11052021.cleaned.pptx
Stakeholders Management GT 11052021.cleaned.pptxStakeholders Management GT 11052021.cleaned.pptx
Stakeholders Management GT 11052021.cleaned.pptx
SaranshJeena
 
Software Development Business Plan1.pptx
Software Development Business Plan1.pptxSoftware Development Business Plan1.pptx
Software Development Business Plan1.pptx
vkprintingsolution
 
LCL216_2024-2_WEEKS 4 & 5_IF CLAUSES (1).pdf
LCL216_2024-2_WEEKS 4 & 5_IF CLAUSES (1).pdfLCL216_2024-2_WEEKS 4 & 5_IF CLAUSES (1).pdf
LCL216_2024-2_WEEKS 4 & 5_IF CLAUSES (1).pdf
rafaelsago2015
 
material-17438335 to the third floor in 47-gsms.pptx
material-17438335 to the third floor in 47-gsms.pptxmaterial-17438335 to the third floor in 47-gsms.pptx
material-17438335 to the third floor in 47-gsms.pptx
JyotirmayNirankari
 
CHAPTER 7 - Foreign Direct Investment.pptx
CHAPTER 7 - Foreign Direct Investment.pptxCHAPTER 7 - Foreign Direct Investment.pptx
CHAPTER 7 - Foreign Direct Investment.pptx
72200337
 
美国学位证(纽约州立大学德里分校毕业证明)SUNYD文凭证书复刻
美国学位证(纽约州立大学德里分校毕业证明)SUNYD文凭证书复刻美国学位证(纽约州立大学德里分校毕业证明)SUNYD文凭证书复刻
美国学位证(纽约州立大学德里分校毕业证明)SUNYD文凭证书复刻
Taqyea
 
Introduction on Speaking skills Power Point
Introduction on Speaking skills Power PointIntroduction on Speaking skills Power Point
Introduction on Speaking skills Power Point
helenswarna
 
Latest Questions & Answers | Prepare for H3C GB0-961 Certification
Latest Questions & Answers | Prepare for H3C GB0-961 CertificationLatest Questions & Answers | Prepare for H3C GB0-961 Certification
Latest Questions & Answers | Prepare for H3C GB0-961 Certification
NWEXAM
 
RightShip-Inspection-Maritime-Safety-Simplified.pptx
RightShip-Inspection-Maritime-Safety-Simplified.pptxRightShip-Inspection-Maritime-Safety-Simplified.pptx
RightShip-Inspection-Maritime-Safety-Simplified.pptx
ultronmeg
 
Top MBA Specializations List at ISMS Pune.pdf
Top MBA Specializations List at ISMS Pune.pdfTop MBA Specializations List at ISMS Pune.pdf
Top MBA Specializations List at ISMS Pune.pdf
ISMS PUne
 
Handling Exceptions and waits in selenium.pptx
Handling Exceptions and waits in selenium.pptxHandling Exceptions and waits in selenium.pptx
Handling Exceptions and waits in selenium.pptx
Madhuri Lonikar
 
sorcesofdrugs-160228074 56 4246643544 (3).ppt
sorcesofdrugs-160228074 56 4246643544 (3).pptsorcesofdrugs-160228074 56 4246643544 (3).ppt
sorcesofdrugs-160228074 56 4246643544 (3).ppt
IndalSatnami
 
History of Entomology and current updates of entomology.pptx
History of Entomology and current updates of entomology.pptxHistory of Entomology and current updates of entomology.pptx
History of Entomology and current updates of entomology.pptx
Neelesh Raipuria
 
Huckel_MO_Theory_Colorful_Presentation (1).pptx
Huckel_MO_Theory_Colorful_Presentation (1).pptxHuckel_MO_Theory_Colorful_Presentation (1).pptx
Huckel_MO_Theory_Colorful_Presentation (1).pptx
study2022bsc
 
2. Basic Legal terminologies in law for paralegal officers .docx
2. Basic Legal terminologies in law for paralegal officers .docx2. Basic Legal terminologies in law for paralegal officers .docx
2. Basic Legal terminologies in law for paralegal officers .docx
JosiahOngai1
 
Organic Reactions Overviewwwweeeeeeee.pptx
Organic Reactions Overviewwwweeeeeeee.pptxOrganic Reactions Overviewwwweeeeeeee.pptx
Organic Reactions Overviewwwweeeeeeee.pptx
JayPatel845511
 
Top Business Schools in Delhi For Quality Education
Top Business Schools in Delhi For Quality EducationTop Business Schools in Delhi For Quality Education
Top Business Schools in Delhi For Quality Education
top10privatecolleges
 
Research Project csi1 - This presentation compares popular web browsers such ...
Research Project csi1 - This presentation compares popular web browsers such ...Research Project csi1 - This presentation compares popular web browsers such ...
Research Project csi1 - This presentation compares popular web browsers such ...
bomisung0207
 
remakingyourselfpresentation-250430095415-6476ade1.pptx
remakingyourselfpresentation-250430095415-6476ade1.pptxremakingyourselfpresentation-250430095415-6476ade1.pptx
remakingyourselfpresentation-250430095415-6476ade1.pptx
lakhmanpindariya9176
 
Huckel_Molecular orbital _Theory_8_Slides.pptx
Huckel_Molecular orbital _Theory_8_Slides.pptxHuckel_Molecular orbital _Theory_8_Slides.pptx
Huckel_Molecular orbital _Theory_8_Slides.pptx
study2022bsc
 
Stakeholders Management GT 11052021.cleaned.pptx
Stakeholders Management GT 11052021.cleaned.pptxStakeholders Management GT 11052021.cleaned.pptx
Stakeholders Management GT 11052021.cleaned.pptx
SaranshJeena
 
Software Development Business Plan1.pptx
Software Development Business Plan1.pptxSoftware Development Business Plan1.pptx
Software Development Business Plan1.pptx
vkprintingsolution
 
LCL216_2024-2_WEEKS 4 & 5_IF CLAUSES (1).pdf
LCL216_2024-2_WEEKS 4 & 5_IF CLAUSES (1).pdfLCL216_2024-2_WEEKS 4 & 5_IF CLAUSES (1).pdf
LCL216_2024-2_WEEKS 4 & 5_IF CLAUSES (1).pdf
rafaelsago2015
 
material-17438335 to the third floor in 47-gsms.pptx
material-17438335 to the third floor in 47-gsms.pptxmaterial-17438335 to the third floor in 47-gsms.pptx
material-17438335 to the third floor in 47-gsms.pptx
JyotirmayNirankari
 
CHAPTER 7 - Foreign Direct Investment.pptx
CHAPTER 7 - Foreign Direct Investment.pptxCHAPTER 7 - Foreign Direct Investment.pptx
CHAPTER 7 - Foreign Direct Investment.pptx
72200337
 
Ad

Ch3_Elements of JAVA Programming.pptx ka

  • 1. Object Oriented Programming Using Java BASIC ELEMENTS OF JAVA PROGRAMMING School of Computer Engineering KIIT Deemed to be University Dr.Sunil Kumar Gouda Asst. Professor
  • 2. 01/25/2025 Java Programming, Sunil Kumar Gouda First JAVA Program Program: To print a message Welcome to the JAVA World class First { public static void main (String args[]) { System.out.println("Welcome to the JAVA world"); } } Output: Welcome to the JAVA world
  • 3. 01/25/2025 Java Programming, Sunil Kumar Gouda How to compile and execute a Java Program  Command to Compile a java program on the command line: javac file_name.java Following command is used to compile the First.java program javac First.java  Syntax to Run/Execute a java program on the command line: java file_name Following command is used to run/ execute the First.java program java First
  • 4. 01/25/2025 Java Programming, Sunil Kumar Gouda Explanation of the above program (First.java)  class First In a purely object oriented programming system, all the codes remain inside a class definition. Therefore, the program begins with a class definition. Here class is a keyword used to define a class & First is the name of our class. { Here this opening brace indicates the beginning of the class definition block.  public static void main(String args[]) All Java applications begin execution from main () function. (This is just like C/C++.) 
  • 5. 01/25/2025 Java Programming, Sunil Kumar Gouda Explanation of the above program (First.java) The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. The keyword static allows main () to be called without having to instantiate a particular instance of the class. The keyword void simply tells the compiler that main () does not return a value. As stated, main () is the method called when a Java application begins. Keep in mind that Java is case-sensitive. Thus, Main is different from main. String args[] declares a parameter named args, which is an array of instances of the class String. This args parameter is essential when we enter some input through the command line while executing a program.
  • 6. 01/25/2025 Java Programming, Sunil Kumar Gouda Explanation of the above program (First.java)  { Here this opening brace indicates the beginning of main method definition block.  System.out.println("Welcome to the JAVA world "); This line outputs the string “Welcome to the JAVA world” followed by a new line on the screen.Output is actually accomplished by the built-in println() method. In this case, println() displays the string which is passed to it.  } Here this closing brace indicates the end of main method definition block.  } Here this closing brace indicates the end of the class definition block.
  • 7. 01/25/2025 Java Programming, Sunil Kumar Gouda Java is a Strongly Typed Language  It is important to state at the outset that Java is a strongly typed language. Main reasons are-  Every variable has a type, every expression has a type, and every type is strictly defined.  All assignments, whether explicit or via parameter passing in method calls, are checked for type compatibility.
  • 8. 01/25/2025 Java Programming, Sunil Kumar Gouda Data Types  Data types specify the different size and value that can be stored in a variable. There are two types of data types in Java:  Primitive data types: Java defines eight primitive types of data. The primitive data types (or simple data types) include boolean, char, byte, short, int, long, float and double which are put in four distinct groups (as shown in the next slide).  Non-primitive data types: These are user defined datatypes. The non- primitive data types include class, interface, and array etc.
  • 9. 01/25/2025 Java Programming, Sunil Kumar Gouda Primitive Data Types Data Type Default Value Size Range Boolean boolean false 1 bit 0 or 1 Character char 'u0000' 2 byte -32768 to 32767 (-215 to 215 -1) Integer byte 0 1 byte -128 to 127 (-27 to 27 -1) short 0 2 byte -32768 to 32767 (-215 to 215 -1) int 0 4 byte -2,147,483,648 to 2,147,483,647 (231 to 231 -1) long 0L 8 byte -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (-263 to 263 -1) Float/ Real float 0.0f 4 byte -231 to 231 -1 double 0.0d 8 byte -263 to 263 -1
  • 10. 01/25/2025 Java Programming, Sunil Kumar Gouda Primitive Data Types Cont.. In general, if n bits are required for a data type, then its range is −2𝑛−1 ¿2𝑛−1 −1
  • 11. 01/25/2025 Java Programming, Sunil Kumar Gouda Java Characters Take 2 Bytes of Memory !!  It is because java uses Unicode system, not ASCII (American Standard Code for Information Interchange) code system.  Unicode system represents a character set which can be used to represent all languages available world. u0000 is the lowest range of Unicode system.
  • 12. 01/25/2025 Java Programming, Sunil Kumar Gouda Java tokens  The smallest unit of code appearing in a program which is recognized by the compiler is called as token.  A token in a java program are of following types: Identifiers Literals/ Constant Keywords Operators Comment Special characters
  • 13. 01/25/2025 Java Programming, Sunil Kumar Gouda Identifiers  These are names assigned to any variable, method, classe, package or interface. Following are the rules to construct an identifier: An identifier contains alphabets, digits, underscore or dollar ($) only. The first character in an identifier must be an alphabet or underscore(_) or dollar($). Blank spaces are not allowed within an identifier. Keywords can’t be identifiers. Identifier name are case sensitive. There is no limit on the length of a Java identifier.  Some valid identifiers are x, a1, $p, my_var etc.
  • 14. 01/25/2025 Java Programming, Sunil Kumar Gouda Literals  Literals in Java are a sequence of characters (digits, letters, and other characters) that represent constant values to be stored in variables. Java offers following types of literals: Integer literals: These are numeric values without a decimal point. Examples of integer literals are 1, -34, 1083 (in decimal) etc. Two other bases that can be used in integer literals are octal (base eight) and hexadecimal (base 16). Octal values are denoted in Java by a leading 0 and hexadecimal hexadecimal values are denoted by leading 0x or 0X. Character literals: Any single character enclosed in a single quote (‘ ’) is termed as a character literal. Examples of character literals are ‘A’, ‘5’, ‘ ’, ‘.’ etc.
  • 15. 01/25/2025 Java Programming, Sunil Kumar Gouda Literals Cont.. Floating literals: These are numeric values with a decimal point and fractional component. Examples of floating literals are 1.97, -34.07, 2009.06 (standard notation), 1.56e+78, 1.09e-9 (scientific notation) etc. Floating-point literals in Java default to double precision. To specify a float literal, we must append an F or f to the constant. We can also explicitly specify a double literal by appending a D or d. Doing so is, of course, redundant. String literals: One or more character enclosed in a double quote (“ ”) is termed as a string literal. Examples of string literals are “Java Programming”, “5” etc. Boolean literals: These are literals which acquire Boolean values true or false. Null literals: The null in Java is a literal of the null type. It cannot be cast to a primitive type such as int. float etc, but can be cast to a reference type. Also, null does not have the value 0 necessarily.
  • 16. 01/25/2025 Java Programming, Sunil Kumar Gouda Keywords  Keywords or Reserved words are the words that represent some predefined actions.  Keywords are therefore not allowed to use as a variable names or objects.  There are 49 keywords in java.  Examples: abstract, boolean, break, byte, case, catch, char, class, continue, default, do, double, else, enum, extends, final, finally, float, for, if, implements, import, instanceof, int, interface, long, native, new, null, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while.
  • 17. 01/25/2025 Java Programming, Sunil Kumar Gouda Operators  Operator in java is a symbol that is used to perform operations on one or more operands.  Depending upon number of operands, operators are classified as- Type of operator Number of operands Example Unary 1 -, ++, -- etc Binary 2 +, -, /, *, % etc Ternary 3 ?:
  • 18. 01/25/2025 Java Programming, Sunil Kumar Gouda Types of Operators  Various types of operators in java are  Arithmetic Operator Relational Operator Logical Operator Increment & decrement Operator Assignment Operator Ternary Operator Bitwise and Shift Operator instanceof operator arrow operator
  • 19. 01/25/2025 Java Programming, Sunil Kumar Gouda Arithmetic Operator Arithmetic operator Yields Example Addition (+) addition result 5+3=8 Subtraction () difference result 5-3=2 Multiplication (*) product result 5*3=15 Division (/) division result 5/3=1 Modular Division (%) remainder result 5%3=2
  • 20. 01/25/2025 Java Programming, Sunil Kumar Gouda Relational Operator Relational Operator Meaning Example > Strictly greater than 2>5 yields false >= Greater than or equal to 5>=4 yields true < Strictly less than 5<100 yields true <= Smaller than or equal to 5<=5 yields true == Double equal to 6==7 yields false != Not equal to 6!=7 yields true
  • 21. 01/25/2025 Java Programming, Sunil Kumar Gouda Logical Operator Operator Meaning && Logical AND, a && b is true when both a and b are true, false otherwise. || Logical OR, a || b is false when both a and b are false, true otherwise. ! Logical NOT, !a is true if a is false and vice- versa.
  • 22. 01/25/2025 Java Programming, Sunil Kumar Gouda Increment/ Decrement Operator Expression Meaning y=x++ Value of x is assigned to y before it is incremented Y=++x Value of x is incremented first and then assigned to y
  • 23. 01/25/2025 Java Programming, Sunil Kumar Gouda Assignment Operator  Assignment operators are used in Java to assign values to variables.  For example, int age=5; The assignment operator assigns the value on its right to the variable on its left. Here, 5 is assigned to the variable age using = operator.  Shorthand assignment operator: Operators like +=, -=, *=, /= etc are known as shorthand assignment operator. x+=1 is equivalent to x=x+1 y/=9 is equivalent to y=y/9 etc.
  • 24. 01/25/2025 Java Programming, Sunil Kumar Gouda Conditional Operator  This operator consists of three operands and is used to evaluate boolean expressions. The goal of this operator is to decide; which value should be assigned to the variable. The operator is written as: variable x = (expression)? value if true:value if false Example: public class Test{ public static void main(String args[]) { int a, b; a = 10; b = (a == 1) ? 20: 30; System.out.println("Value of b is: " + b); b = (a == 10) ? 20: 30; System.out.println(“Value of b is: " + b); } } Output: Value of b is: 30 Value of b is: 20
  • 25. 01/25/2025 Java Programming, Sunil Kumar Gouda Comment Line  Comments are statements that are not executed by the compiler and interpreter.  The comments can be used to provide information or explanation about the variable, method, class or any statement.  These can also be used to hide program code for specific time.  In Java there are three types of comments:  Single line comments (//single line of text)  Multi line comments (/*multiple lines of text /*)  Documentation comments (/** documentation text*/)
  • 26. 01/25/2025 Java Programming, Sunil Kumar Gouda Type Casting  The method of converting one primitive data type into another data type is called type casting.  There are two types of typecasting: Implicit type casting or Widening casting Explicit type casting or Narrowing casting
  • 27. 01/25/2025 Java Programming, Sunil Kumar Gouda Implicit type casting or Widening casting  Implicit type casting or widening casting:This is the process of converting a smaller type to a larger type. When one type of data is assigned to another type of variable, an automatic type conversion will take place if the following two conditions are met: The two types are compatible. The destination type is larger than the source type.  For widening conversions, the numeric types, including integer and floating point types, are compatible with each other. However, there are no automatic conversions from the numeric types to char or boolean. Also, char and boolean are not compatible with each other.
  • 28. 01/25/2025 Java Programming, Sunil Kumar Gouda Implicit type casting or Widening casting Cont.. Example: public class ImplicitTC { public static void main(String[] args) { int myInt = 9; double myDouble = myInt; System.out.println(myInt); System.out.println(myDouble); } } Output: 9 9.0
  • 29. 01/25/2025 Java Programming, Sunil Kumar Gouda Explicit type casting or Narrowing casting  This type casting is done manually by placing the type in parentheses in front of the value. This is done manually by the programmer. Hence it is also known as explicit type casting.  For example, the following fragment casts an int to a byte. If the integer’s value is larger than the range of a byte, it will be reduced modulo to (the remainder of an integer division by the) byte’s range. int a; byte b; // … b = (byte) a;  A different type of conversion will occur when a floating-point value is assigned to an integer type: truncation.
  • 30. 01/25/2025 Java Programming, Sunil Kumar Gouda Explicit type casting or Narrowing casting Cont.. Example: public class ExplicitTC { public static void main(String[] args) { double myDouble = 9.78; int myInt = (int) myDouble; System.out.println(myDouble); System.out.println(myInt); } } Output: 9.78 9
  • 31. 01/25/2025 Java Programming, Sunil Kumar Gouda Automatic Type Promotion in Expressions  Consider following code: byte a = 40; byte b = 50; byte c = 100; int d = a * b / c;  The result of the intermediate term a*b easily exceeds the range of either of its byte operands.  To handle this kind of problem, Java automatically promotes each byte, short, or char operand to int when evaluating an expression.  This means that the sub expression a*b is performed using integers— not bytes. Thus, 2,000, the result of the intermediate expression, 50*40, is legal even though a and b are both specified as type byte.
  • 32. 01/25/2025 Java Programming, Sunil Kumar Gouda Automatic Type Promotion in Expressions Contd..  Consider following code: byte b = 50; b = b * 2; // Error! Cannot assign an int to a byte without cast !  The code is attempting to store 50*2, a perfectly valid byte value, back into a byte variable.  However, because the operands were automatically promoted to int when the expression was evaluated, the result has also been promoted to int.  Thus, the result of the expression is now of type int, which cannot be assigned to a byte without the use of type casting.
  • 33. 01/25/2025 Java Programming, Sunil Kumar Gouda Type Promotion Rules  Java defines several type promotion rules that apply to expressions. First, all byte, short, and char values are promoted to int. Then, if one operand is a long, the whole expression is promoted to long. If one operand is a float, the entire expression is promoted to float. If any of the operands are double, the result is double.
  • 34. 01/25/2025 Java Programming, Sunil Kumar Gouda Array  An array is a group of like-typed variables that are referred to by a common name.  Arrays of any type can be created and may have one or more dimensions.  A specific element in an array is accessed by its index. Arrays offer a convenient means of grouping related information.  Different types of arrays are: One dimensional (1D) array Multi dimensional (nD) array
  • 35. 01/25/2025 Java Programming, Sunil Kumar Gouda One Dimensional (1-D) Array  The general form of a one-dimensional array declaration is: data_type var-name[ ];  For example, the following declares an array named month_days with the type “array of int”: int month_days[];  To allocate memory for arrays, new operator is applied to one- dimensional arrays as follows: array-var = new type [size];  Therefore, obtaining an array is a two-step process. First, we must declare a variable of the desired array type. Second, we must allocate the memory that will hold the array, using new, and assign it to the array variable.  Thus, in Java all arrays are dynamically allocated.
  • 36. 01/25/2025 Java Programming, Sunil Kumar Gouda Multi Dimensional (n-D) Array  In Java, multidimensional arrays are implemented as array of arrays.  To declare a multidimensional array variable, specify each additional index using another set of square brackets.  For example, the following declares a two dimensional array variable called twoD: int twoD[][] = new int[4][5];
  • 37. 01/25/2025 Java Programming, Sunil Kumar Gouda Alternative Array Declaration Syntax  There is a second form that may be used to declare an array: type[] var-name;  Following two declarations are equivalent: int al[] = new int[3]; int[] a2 = new int[3];  The following declarations are also equivalent: char twod1[][] = new char[3][4]; char[][] twod2 = new char[3][4];
  • 38. 01/25/2025 Java Programming, Sunil Kumar Gouda END OF THE CHAPTER