SlideShare a Scribd company logo
Introduction to java
What is Java?
• Java is a popular programming and Object Oriented Programming language, created in 1995.
• It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
History of Java
• Java was developed by James Gosling, who is known as the father of Java, in 1995. James Gosling and his team
members started the project in the early '90s.
1. 1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. The
small team of sun engineers called Green Team.
2. Initially it was designed for small, embedded systems in electronic appliances like set-top boxes.
3. Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
4. 4) After that, it was called Oak and was developed as a part of the Green project.
5. In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.
6. The first public version, Java 1.0, was released in 1996 and could run on many devices for free.
Why Use Java?
• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• It is one of the most popular programming languages in the world
• It has a large demand in the current job market
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful.
• Java is an object oriented language which gives a clear structure to programs and
allows code to be reused, lowering development costs
Basic Concept of OOPS
1.Class
• Classes are a fundamental concept in object-oriented programming (OOP).
• A class can be understood as a template or blueprint of attributes and functions,
which contains some values, known as data members, and some set of rules,
known as behaviors or methods.
Syntax for class
class ClassName
{
// Data members
public:
// Member functions
};
2.Object
Objects are the basic unit of OOPS representing real-life entities. They are
invoked with the help of methods. These methods are declared within a
class.
The properties of objects are often referred to as variables of the object,
and behaviors are referred to as the functions of the objects.
3. Abstraction
• Abstraction means showing only the relevant details to the end-user and
hiding the irrelevant features that serve as a distraction.
• For example, during an ATM operation, we only answer a series of
questions to process the transaction without any knowledge about what
happens in the background between the bank and the ATM.
4. Encapsulation
• Encapsulation is a means of binding data variables and methods
together in a class.
• Only objects of the class can then be allowed to access these entities.
This is known as data hiding and helps in the insulation of data.
5.Inheritance
• Inheritance is the process by which one class inherits the functions
and properties of another class.
• The main function of inheritance is the reusability of code. Each
subclass only has to define its features.
• The rest of the features can be derived directly from the parent class.
6.Polymorphism:
• Polymorphism refers to many forms, or it is a process that performs a single
action in different ways.
• For example, “+” can be used for addition as well as string concatenation.
Java Syntax:
public class Main
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
1. Every line of code that runs in Java must be inside a
class.
2. A class should always start with an uppercase first
letter.
3. The name of the java file must match the class
name.
4. When saving the file, save it using the class name
and add ".java" to the end of the filename.
The main Method:
The main() method is required for every Java program:
public static void main(String[] args)
Any code inside the main() method will be executed.
System.out.println()
System is a built-in Java class that contains useful members, such as out, which
is short for "output".
The println() method, short for "print line", is used to print a value to the screen
(or a file).
Java Output:
In java, there are two methods are used to print the output on screen
Print - print the output on screen
Println-print the output on screen and insert the newline at the end of the output.
Java Comments:
Comments can be used to explain Java code, and to make it more readable. It can also be used
to prevent execution when testing alternative code.
Any text between the comments will be ignored by java.
Single line comments()
Multi line comments(* text */)
Java Variables
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
1. String - stores text, such as "Hello". String values are surrounded by double
quotes
2. int - stores integers (whole numbers), without decimals, such as 123 or -123
3. float - stores floating point numbers, with decimals, such as 19.99 or -19.99
4. char - stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes
5. boolean - stores values with two states: true or false
Declaring (Creating) Variables
• To create a variable, you must specify the type and assign it a value:
Syntax: type variableName = value;
• Where type is one of Java's types (such as int or String), and variableName is the
name of the variable (such as x or name). The equal sign is used to assign values
to the variable.
String studentName = "John Doe";
int studentID = 15;
int studentAge = 23;
float studentFee = 75.25f;
char studentGrade = 'B';
// Print variables
System.out.println("Student name: " + studentName);
System.out.println("Student id: " + studentID);
System.out.println("Student age: " + studentAge);
System.out.println("Student fee: " + studentFee);
System.out.println("Student grade: " + studentGrade);
Java Identifiers:
• All Java variables must be identified with unique names.
• These unique names are called identifiers.
The general rules for naming variables are:
1. Names can contain letters, digits, underscores, and dollar signs
2. Names must begin with a letter
3. Names should start with a lowercase letter, and cannot contain whitespace
4. Names can also begin with $ and _ (but we will not use it in this tutorial)
5. Names are case-sensitive ("myVar" and "myvar" are different variables)
6. Reserved words (like Java keywords, such as int or boolean) cannot be used as names
Datatypes in java
Data types are divided into two groups:
• Primitive data types
• Non-primitive data types
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to
2,147,483,647
long 8 bytes Stores whole numbers from -
9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6
to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing
15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values
Primitive Data Types:
• A primitive data type specifies the size and type of variable values.
• There are eight primitive data types in Java:
Numbers
Primitive number types are divided into two groups:
• Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int and long.
Which type you should use, depends on the numeric value.
• Floating point types represents numbers with a fractional part, containing one or more decimals. There are two types: float and double.
• The precision of float is only six or seven decimal digits, while double variables have a precision of about 15
digits.
public class Main
{
public static void main(String[] args)
{
float f1 = 35e3f;
double d1 = 12E4d;
System.out.println(f1);
System.out.println(d1);
}
}
Java Boolean Data Types
Java has a Boolean datatype which only take values TRUE or FALSE.
Boolean values are mostly used for conditional testing.
Characters
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':
We can also display the character using ascii value.
char myGrade = 'B';
System.out.println(myGrade);
char myVar1 = 65, myVar2 = 66, myVar3 = 67;
System.out.println(myVar1);
System.out.println(myVar2);
System.out.println(myVar3);
Unicode System in Java
• Java programming language, being platform-independent, has built-in support for Unicode characters,
allowing developers to create applications that can work seamlessly with diverse languages and
scripts.
• Before Unicode, there were multiple standards to represent character encoding −
• ASCII − for the United States.
• ISO 8859-1 − for Western European Language.
• KOI-8 − for Russian.
• GB18030 and BIG-5 − for Chinese.
• So to support multinational application codes, some character was using single byte, some two. An
even same code may represent a different character in one language and may represent other
characters in another language.
• To overcome above shortcoming, the Unicode system was developed where each character is
represented by 2 bytes.
• The lowest value is represented by u0000 and the highest value is represented by uFFFF.
Approaches: Working with Unicode Characters & Values
There are two approaches for working with Unicode characters in Java:
1) Using Unicode Escape Sequences
2) Directly Storing Unicode Characters.
1. Using Unicode Escape Sequences
An escape sequence is a series of characters that represent a special character.
In Java, a Unicode escape sequence starts with the characters 'u' followed by four hexadecimal digits that represent the
Unicode code point of the desired character.
public class Unicode_sample
{
public static void main (String[]args)
{
//Unicode escape sequence
char unicodeChar = 'u0041';
// point for 'A‘
System.out.println("Stored Unicode Character: " + unicodeChar);
}
}
2. Storing Unicode Values Directly
public class UnicodeCharacterDemo
{
public static void main(String[] args)
{
// Storing Unicode character directly
char unicodeChar = 'A';
// Directly storing the character 'A'
System.out.println("Stored Unicode Character: " + unicodeChar);
}
}
Strings
The String data type is used to store a sequence of characters (text).
String values must be surrounded by double quotes.
A String in Java is actually a non-primitive data type, because it refers to an object. The String
object has methods that are used to perform certain operations on strings.
String greeting = "Hello World";
System.out.println(greeting);
Non-Primitive Data Types
Non-primitive data types are called reference types because they refer to objects.
The main difference between primitive and non-primitive data types are:
• Primitive types are predefined (already defined) in Java. Non-primitive types are created by the
programmer and is not defined by Java (except for String).
• Non-primitive types can be used to call methods to perform certain operations, while primitive types
cannot.
• A primitive type has always a value, while non-primitive types can be null.
• A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
Java Type Casting
• In Java, type casting is a method or process that converts a data type
into another data type in both ways manually and automatically.
• The automatic conversion is done by the compiler and manual
conversion performed by the programmer.
Types of Type Casting
There are two types of type casting:
• Widening Type Casting
• Narrowing Type Casting
Widening Type Casting
• Converting a lower data type into a higher one is called widening type casting.
• It is also known as implicit conversion or casting down. It is done automatically.
• It is safe because there is no chance to lose data.
It takes place when:
• Both data types must be compatible with each other.
• The target type must be larger than the source type.
Narrowing Type Casting
• Converting a higher data type into a lower one is called narrowing type casting.
• It is also known as explicit conversion or casting up.
• It is done manually by the programmer.
• If we do not perform casting then the compiler reports a compile-time error.
Example:
double d = 166.66;
//converting double data type into long data type
long l = (long)d;
//converting long data type into int data type
int i = (int)l;
System.out.println("Before conversion: "+d);
//fractional part lost
System.out.println("After conversion into long type: "+l);
System.out.println("After conversion into int type: "+i);
Narrowing casting must be done manually by placing the type in parentheses () in front of the value:

More Related Content

Similar to Introduction to java Programming Language (20)

PPTX
Java Basics.pptx from nit patna ece department
om2348023vats
 
PPTX
OOP-java-variables.pptx
ssuserb1a18d
 
PPTX
Java fundamentals
Jayfee Ramos
 
PPTX
intro_java (1).pptx
SmitNikumbh
 
PPT
Introduction to-programming
BG Java EE Course
 
PPTX
Chapter 2 java
Ahmad sohail Kakar
 
PPTX
Basics of java 2
Raghu nath
 
PDF
java notes.pdf
JitendraYadav351971
 
PPSX
DISE - Windows Based Application Development in Java
Rasan Samarasinghe
 
PPTX
Java programming(unit 1)
Dr. SURBHI SAROHA
 
PPT
Java SpringMVC SpringBOOT (Divergent).ppt
Aayush Chimaniya
 
PPTX
L2 datatypes and variables
teach4uin
 
PPTX
JAVA LESSON-01.pptx
StephenOczon1
 
PPTX
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 
PPTX
Android webinar class_java_review
Edureka!
 
ODP
Introduction To Java.
Tushar Chauhan
 
PPTX
Modern_2.pptx for java
MayaTofik
 
PPTX
Java platform
Visithan
 
PPT
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 
PPT
demo1 java of demo 1 java with demo 1 java.ppt
FerdieBalang
 
Java Basics.pptx from nit patna ece department
om2348023vats
 
OOP-java-variables.pptx
ssuserb1a18d
 
Java fundamentals
Jayfee Ramos
 
intro_java (1).pptx
SmitNikumbh
 
Introduction to-programming
BG Java EE Course
 
Chapter 2 java
Ahmad sohail Kakar
 
Basics of java 2
Raghu nath
 
java notes.pdf
JitendraYadav351971
 
DISE - Windows Based Application Development in Java
Rasan Samarasinghe
 
Java programming(unit 1)
Dr. SURBHI SAROHA
 
Java SpringMVC SpringBOOT (Divergent).ppt
Aayush Chimaniya
 
L2 datatypes and variables
teach4uin
 
JAVA LESSON-01.pptx
StephenOczon1
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 
Android webinar class_java_review
Edureka!
 
Introduction To Java.
Tushar Chauhan
 
Modern_2.pptx for java
MayaTofik
 
Java platform
Visithan
 
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 
demo1 java of demo 1 java with demo 1 java.ppt
FerdieBalang
 

Recently uploaded (20)

PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Ad

Introduction to java Programming Language

  • 2. What is Java? • Java is a popular programming and Object Oriented Programming language, created in 1995. • It is owned by Oracle, and more than 3 billion devices run Java. It is used for: • Mobile applications (specially Android apps) • Desktop applications • Web applications • Web servers and application servers • Games • Database connection
  • 3. History of Java • Java was developed by James Gosling, who is known as the father of Java, in 1995. James Gosling and his team members started the project in the early '90s. 1. 1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. The small team of sun engineers called Green Team. 2. Initially it was designed for small, embedded systems in electronic appliances like set-top boxes. 3. Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt. 4. 4) After that, it was called Oak and was developed as a part of the Green project. 5. In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies. 6. The first public version, Java 1.0, was released in 1996 and could run on many devices for free.
  • 4. Why Use Java? • Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) • It is one of the most popular programming languages in the world • It has a large demand in the current job market • It is easy to learn and simple to use • It is open-source and free • It is secure, fast and powerful. • Java is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs
  • 5. Basic Concept of OOPS 1.Class • Classes are a fundamental concept in object-oriented programming (OOP). • A class can be understood as a template or blueprint of attributes and functions, which contains some values, known as data members, and some set of rules, known as behaviors or methods. Syntax for class class ClassName { // Data members public: // Member functions };
  • 6. 2.Object Objects are the basic unit of OOPS representing real-life entities. They are invoked with the help of methods. These methods are declared within a class. The properties of objects are often referred to as variables of the object, and behaviors are referred to as the functions of the objects. 3. Abstraction • Abstraction means showing only the relevant details to the end-user and hiding the irrelevant features that serve as a distraction. • For example, during an ATM operation, we only answer a series of questions to process the transaction without any knowledge about what happens in the background between the bank and the ATM.
  • 7. 4. Encapsulation • Encapsulation is a means of binding data variables and methods together in a class. • Only objects of the class can then be allowed to access these entities. This is known as data hiding and helps in the insulation of data. 5.Inheritance • Inheritance is the process by which one class inherits the functions and properties of another class. • The main function of inheritance is the reusability of code. Each subclass only has to define its features. • The rest of the features can be derived directly from the parent class.
  • 8. 6.Polymorphism: • Polymorphism refers to many forms, or it is a process that performs a single action in different ways. • For example, “+” can be used for addition as well as string concatenation.
  • 9. Java Syntax: public class Main { public static void main(String args[]) { System.out.println("Hello World"); } } 1. Every line of code that runs in Java must be inside a class. 2. A class should always start with an uppercase first letter. 3. The name of the java file must match the class name. 4. When saving the file, save it using the class name and add ".java" to the end of the filename.
  • 10. The main Method: The main() method is required for every Java program: public static void main(String[] args) Any code inside the main() method will be executed. System.out.println() System is a built-in Java class that contains useful members, such as out, which is short for "output". The println() method, short for "print line", is used to print a value to the screen (or a file).
  • 11. Java Output: In java, there are two methods are used to print the output on screen Print - print the output on screen Println-print the output on screen and insert the newline at the end of the output. Java Comments: Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Any text between the comments will be ignored by java. Single line comments() Multi line comments(* text */)
  • 12. Java Variables Variables are containers for storing data values. In Java, there are different types of variables, for example: 1. String - stores text, such as "Hello". String values are surrounded by double quotes 2. int - stores integers (whole numbers), without decimals, such as 123 or -123 3. float - stores floating point numbers, with decimals, such as 19.99 or -19.99 4. char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes 5. boolean - stores values with two states: true or false
  • 13. Declaring (Creating) Variables • To create a variable, you must specify the type and assign it a value: Syntax: type variableName = value; • Where type is one of Java's types (such as int or String), and variableName is the name of the variable (such as x or name). The equal sign is used to assign values to the variable. String studentName = "John Doe"; int studentID = 15; int studentAge = 23; float studentFee = 75.25f; char studentGrade = 'B'; // Print variables System.out.println("Student name: " + studentName); System.out.println("Student id: " + studentID); System.out.println("Student age: " + studentAge); System.out.println("Student fee: " + studentFee); System.out.println("Student grade: " + studentGrade);
  • 14. Java Identifiers: • All Java variables must be identified with unique names. • These unique names are called identifiers. The general rules for naming variables are: 1. Names can contain letters, digits, underscores, and dollar signs 2. Names must begin with a letter 3. Names should start with a lowercase letter, and cannot contain whitespace 4. Names can also begin with $ and _ (but we will not use it in this tutorial) 5. Names are case-sensitive ("myVar" and "myvar" are different variables) 6. Reserved words (like Java keywords, such as int or boolean) cannot be used as names
  • 15. Datatypes in java Data types are divided into two groups: • Primitive data types • Non-primitive data types
  • 16. Data Type Size Description byte 1 byte Stores whole numbers from -128 to 127 short 2 bytes Stores whole numbers from -32,768 to 32,767 int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647 long 8 bytes Stores whole numbers from - 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits boolean 1 bit Stores true or false values char 2 bytes Stores a single character/letter or ASCII values Primitive Data Types: • A primitive data type specifies the size and type of variable values. • There are eight primitive data types in Java:
  • 17. Numbers Primitive number types are divided into two groups: • Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int and long. Which type you should use, depends on the numeric value. • Floating point types represents numbers with a fractional part, containing one or more decimals. There are two types: float and double. • The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. public class Main { public static void main(String[] args) { float f1 = 35e3f; double d1 = 12E4d; System.out.println(f1); System.out.println(d1); } }
  • 18. Java Boolean Data Types Java has a Boolean datatype which only take values TRUE or FALSE. Boolean values are mostly used for conditional testing. Characters The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c': We can also display the character using ascii value. char myGrade = 'B'; System.out.println(myGrade); char myVar1 = 65, myVar2 = 66, myVar3 = 67; System.out.println(myVar1); System.out.println(myVar2); System.out.println(myVar3);
  • 19. Unicode System in Java • Java programming language, being platform-independent, has built-in support for Unicode characters, allowing developers to create applications that can work seamlessly with diverse languages and scripts. • Before Unicode, there were multiple standards to represent character encoding − • ASCII − for the United States. • ISO 8859-1 − for Western European Language. • KOI-8 − for Russian. • GB18030 and BIG-5 − for Chinese. • So to support multinational application codes, some character was using single byte, some two. An even same code may represent a different character in one language and may represent other characters in another language. • To overcome above shortcoming, the Unicode system was developed where each character is represented by 2 bytes. • The lowest value is represented by u0000 and the highest value is represented by uFFFF.
  • 20. Approaches: Working with Unicode Characters & Values There are two approaches for working with Unicode characters in Java: 1) Using Unicode Escape Sequences 2) Directly Storing Unicode Characters. 1. Using Unicode Escape Sequences An escape sequence is a series of characters that represent a special character. In Java, a Unicode escape sequence starts with the characters 'u' followed by four hexadecimal digits that represent the Unicode code point of the desired character. public class Unicode_sample { public static void main (String[]args) { //Unicode escape sequence char unicodeChar = 'u0041'; // point for 'A‘ System.out.println("Stored Unicode Character: " + unicodeChar); } }
  • 21. 2. Storing Unicode Values Directly public class UnicodeCharacterDemo { public static void main(String[] args) { // Storing Unicode character directly char unicodeChar = 'A'; // Directly storing the character 'A' System.out.println("Stored Unicode Character: " + unicodeChar); } }
  • 22. Strings The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes. A String in Java is actually a non-primitive data type, because it refers to an object. The String object has methods that are used to perform certain operations on strings. String greeting = "Hello World"; System.out.println(greeting);
  • 23. Non-Primitive Data Types Non-primitive data types are called reference types because they refer to objects. The main difference between primitive and non-primitive data types are: • Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except for String). • Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot. • A primitive type has always a value, while non-primitive types can be null. • A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
  • 24. Java Type Casting • In Java, type casting is a method or process that converts a data type into another data type in both ways manually and automatically. • The automatic conversion is done by the compiler and manual conversion performed by the programmer. Types of Type Casting There are two types of type casting: • Widening Type Casting • Narrowing Type Casting
  • 25. Widening Type Casting • Converting a lower data type into a higher one is called widening type casting. • It is also known as implicit conversion or casting down. It is done automatically. • It is safe because there is no chance to lose data. It takes place when: • Both data types must be compatible with each other. • The target type must be larger than the source type.
  • 26. Narrowing Type Casting • Converting a higher data type into a lower one is called narrowing type casting. • It is also known as explicit conversion or casting up. • It is done manually by the programmer. • If we do not perform casting then the compiler reports a compile-time error.
  • 27. Example: double d = 166.66; //converting double data type into long data type long l = (long)d; //converting long data type into int data type int i = (int)l; System.out.println("Before conversion: "+d); //fractional part lost System.out.println("After conversion into long type: "+l); System.out.println("After conversion into int type: "+i); Narrowing casting must be done manually by placing the type in parentheses () in front of the value: