SlideShare a Scribd company logo
Java Session-3
Static variables and Methods
❖
❖

Variables and methods marked as static belongs to class, rather than any particular
instance.
One copy is shared across all class instances.
➢

❖
❖
❖
❖
●

Ex : https://gist.github.com/rajeevprasanna/8754235

A static method can’t access a nonstatic(instance) variable, because there is not
instance. ex : https://gist.github.com/rajeevprasanna/8754328
Static=class, nonstatic=instance.
Access static method or variable by dot operator on className.
Static methods can’t be overridden.
ex : https://gist.github.com/rajeevprasanna/8754437
Static or initialization block: Initialization blocks run when the class is first loaded (a static
initialization block) or when an instance is created. ex : https://gist.github.com/rajeevprasanna/8756880
Data types and primitives
Data Types : Primitive types are special data types built into the language; they
are not objects created from a class
Literal : A Literal is the source code representation of a fixed value; literals are
represented directly in your code without requiring computation
boolean result = true;
boolean - is data type
true - is literal
Refer : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Float and Double
➔
➔
➔

A float is a 32 bit IEEE 754 floating point.
A double is a 64 bit IEEE 754 floating point.
You shouldn't ever compare floats or doubles for equality; because, you
can't really guarantee that the number you assign to the float or double is
exact.
Ex : https://gist.github.com/rajeevprasanna/8754840
Character literals
●
●

Char literal is represented by a single character in single quotes
It stores the 16-bit Unicode integer value of the character in question
■ char a = 'a';
■ char letterN = 'u004E'; // The letter 'N' in unicode representation
■ char a = 0x892;
// hexadecimal literal
■ char b = 982;
// int literal
■ char c = (char)70000; // The cast is required; 70000 is out of char range
■ char d = (char) -98; // Ridiculous, but legal
■ char e = -29; //Error : Possible loss of precision; needs a cast
■ char f = 70000 //Error : Possible loss of precision; needs a cast

●

Use an escape code if you want to represent a character that can't be typed in as a literal,
including the characters for line feed, newline, horizontal tab, backspace, and single quotes.
■ char c = '"'; // A double quote

■

●

char d = 'n'; // A newline
If given character is out of range, it is displayed as ?. Refer table here
○ Ex : https://gist.github.com/rajeevprasanna/8755247
Assignment operators
●
●

Result of an expression involving anything int-sized or smaller is always an int
Multiply an int and a short and you'll get an int. Divide a short by a byte and you'll get...an int.

ex : https://gist.github.com/rajeevprasanna/8755470
●

●
●

Assigning Floating-Point Numbers : In order to assign a floating-point literal to a
float variable, you must either cast the value or append an f to the end of the literal(compiler
treat as double if no casting is there).
○
float f = (float) 32.3
○
float g = 32.3f
○
float h = 32.3F
byte a = 128; // Error : byte can only hold up to 127(Assigning a Literal That Is Too Large for the
Variable)
Array instance variable initialization : Array elements are always, always, always given
default values, regardless of where the array itself is declared or instantiated.
Passing variables into methods
❖
❖
❖

Passing primitive variables
Passing reference variables
Does java use Pass-By-Value ?
➢

❖
❖

Java is actually pass-by-value for all variables running within a single VM. Pass-by-value
means pass-by-variable-value. And that means, pass-by-copy-of- the-variable!
➢ ex : https://gist.github.com/rajeevprasanna/8755774
Shadowing instance/static primitive variables.
➢ ex: https://gist.github.com/rajeevprasanna/8755805
Shadow instance reference variables.
➢ ex : https://gist.github.com/rajeevprasanna/8755845
Arrays
❖
❖
❖
❖

Declaring arrays
➢ int[] key;//preferred, int key []; (primitive array declaration)
➢ Thread[] threads; //Declaring array of object references.
Array object is created on the heap.
You must specify the size of the array at creation time. The size of the array is the number of
elements the array will hold.
One dimensional array :
➢ int[] testScores;
// Declares the array of ints
testScores = new int[4];

❖

❖
❖

➢ int[] testScores = new int[4];//Initialize at the time of declaration.
➢ int[] carList = new int[]; // Will not compile; needs a size
Multi dimensional array :
➢ Multidimensional arrays, remember, are simply arrays of arrays.
➢ So a two- dimensional array of type int is really an object of type int array (int []), with each
element in that array holding a reference to another int array.
➢ int[][] myArray = new int[3][]; //valid
Array initialization: https://gist.github.com/rajeevprasanna/8756729
Array index starts from 0 to arrayLength-1.
Wrapper class
The wrapper classes in the Java API serve two primary purposes:
●

●

●
●

To provide a mechanism to "wrap" primitive values in an object so that the primitives can be
included in activities reserved for objects, like being added to Collections, or returned from a
method with an object return value.
To provide an assortment of utility functions for primitives. Most of these functions are related
to various conversions: converting primitives to and from String objects, and converting
primitives and String objects to and from different bases (or radix), such as binary, octal, and
hexadecimal.
Wrapper objects are immutable. Once they have been given a value, that value cannot be
changed.
Methods : valueOf, xxxValue, toXxxString,
Overloading
●
●
●
●

Widening. ex : https://gist.github.com/rajeevprasanna/8757265
Autoboxing
Var-args
When overloading, compiler will choose widening over boxing.
○ Widening beats boxing
○ Widening beats var-args
○ Boxing beats var-args
○

●

ex : https://gist.github.com/rajeevprasanna/8757252

Widening reference variables. ex: https://gist.github.com/rajeevprasanna/8757479
Equality operator
❖
❖
❖
❖

== equals (also known as "equal to")
!= not equals (also known as "not equal to")
Equal operation on primitives, references and enums.
➢ ex: https://gist.github.com/rajeevprasanna/8757682
instanceof Comparison:
➢

❖

The instanceof operator is used for object reference variables only, and you can use it to
check whether an object is of a particular type.
➢ By type, we mean class or interface type—in other words, if the object referred to by the
variable on the left side of the operator passes the IS-A test for the class or interface type
on the right side
■ ex: https://gist.github.com/rajeevprasanna/8757785
➢ You can't use the instanceof operator to test across two different class hierarchies.
■ ex: https://gist.github.com/rajeevprasanna/8757802
Remember that arrays are objects, even if the array is an array of primitives
➢ int [] nums = new int[3]; if (nums instanceof Object) { } // result is true

More Related Content

What's hot

Lesson3
Lesson3Lesson3
Lesson3
Arpan91
 
Swift for-rubyists
Swift for-rubyistsSwift for-rubyists
Swift for-rubyists
Michael Yagudaev
 
JavaScript Execution Context
JavaScript Execution ContextJavaScript Execution Context
JavaScript Execution Context
Juan Medina
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Rangana Sampath
 
JavaScript For CSharp Developer
JavaScript For CSharp DeveloperJavaScript For CSharp Developer
JavaScript For CSharp Developer
Sarvesh Kushwaha
 
Introduction of basics loop and array
Introduction of basics loop and arrayIntroduction of basics loop and array
Introduction of basics loop and array
projectsinfo
 
Ruby
RubyRuby
An introduction to javascript
An introduction to javascriptAn introduction to javascript
An introduction to javascript
MD Sayem Ahmed
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming PatternsVasil Remeniuk
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)mircodotta
 
PHP
PHPPHP
Working with Cocoa and Objective-C
Working with Cocoa and Objective-CWorking with Cocoa and Objective-C
Working with Cocoa and Objective-C
Kazunobu Tasaka
 
Introduction to Object Oriented Javascript
Introduction to Object Oriented JavascriptIntroduction to Object Oriented Javascript
Introduction to Object Oriented Javascript
nodeninjas
 
The JavaScript Programming Primer
The JavaScript  Programming PrimerThe JavaScript  Programming Primer
The JavaScript Programming Primer
Mike Wilcox
 
Developing Android applications with Ceylon
Developing Android applications with Ceylon Developing Android applications with Ceylon
Developing Android applications with Ceylon
Enrique Zamudio López
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3Chris Farrell
 
Js
JsJs

What's hot (20)

Lesson3
Lesson3Lesson3
Lesson3
 
Swift for-rubyists
Swift for-rubyistsSwift for-rubyists
Swift for-rubyists
 
JavaScript Execution Context
JavaScript Execution ContextJavaScript Execution Context
JavaScript Execution Context
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
강의자료8
강의자료8강의자료8
강의자료8
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
JavaScript For CSharp Developer
JavaScript For CSharp DeveloperJavaScript For CSharp Developer
JavaScript For CSharp Developer
 
Introduction of basics loop and array
Introduction of basics loop and arrayIntroduction of basics loop and array
Introduction of basics loop and array
 
Ruby
RubyRuby
Ruby
 
An introduction to javascript
An introduction to javascriptAn introduction to javascript
An introduction to javascript
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)
 
PHP
PHPPHP
PHP
 
Working with Cocoa and Objective-C
Working with Cocoa and Objective-CWorking with Cocoa and Objective-C
Working with Cocoa and Objective-C
 
Introduction to Object Oriented Javascript
Introduction to Object Oriented JavascriptIntroduction to Object Oriented Javascript
Introduction to Object Oriented Javascript
 
The JavaScript Programming Primer
The JavaScript  Programming PrimerThe JavaScript  Programming Primer
The JavaScript Programming Primer
 
Developing Android applications with Ceylon
Developing Android applications with Ceylon Developing Android applications with Ceylon
Developing Android applications with Ceylon
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3
 
Js
JsJs
Js
 

Viewers also liked

Cinematica
CinematicaCinematica
Cinematica
campustralenuvole
 
Cookies & Session
Cookies & SessionCookies & Session
Cookies and Session
Cookies and SessionCookies and Session
Cookies and Session
KoraStats
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
Programmer Blog
 
Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookies
www.netgains.org
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
Lena Petsenchuk
 
Free Download Powerpoint Slides
Free Download Powerpoint SlidesFree Download Powerpoint Slides
Free Download Powerpoint Slides
George
 

Viewers also liked (7)

Cinematica
CinematicaCinematica
Cinematica
 
Cookies & Session
Cookies & SessionCookies & Session
Cookies & Session
 
Cookies and Session
Cookies and SessionCookies and Session
Cookies and Session
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
 
Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookies
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
Free Download Powerpoint Slides
Free Download Powerpoint SlidesFree Download Powerpoint Slides
Free Download Powerpoint Slides
 

Similar to Java session 3

Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
mdfkhan625
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java conceptsChikugehlot
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
Daman Toor
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
Ram132
 
Java, what's next?
Java, what's next?Java, what's next?
Java, what's next?
Stephan Janssen
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
Ratnala Charan kumar
 
Learning core java
Learning core javaLearning core java
Learning core java
Abhay Bharti
 
C++ theory
C++ theoryC++ theory
C++ theory
Shyam Khant
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Rahul Jain
 
javascript client side scripting la.pptx
javascript client side scripting la.pptxjavascript client side scripting la.pptx
javascript client side scripting la.pptx
lekhacce
 
Railroading into Scala
Railroading into ScalaRailroading into Scala
Railroading into Scala
Nehal Shah
 
Pj01 3-java-variable and data types
Pj01 3-java-variable and data typesPj01 3-java-variable and data types
Pj01 3-java-variable and data types
SasidharaRaoMarrapu
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3
Mahmoud Alfarra
 
Java Script
Java ScriptJava Script
Java Script
Sarvan15
 

Similar to Java session 3 (20)

Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Scala - core features
Scala - core featuresScala - core features
Scala - core features
 
Java, what's next?
Java, what's next?Java, what's next?
Java, what's next?
 
Core Java
Core JavaCore Java
Core Java
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
Learning core java
Learning core javaLearning core java
Learning core java
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
javascript client side scripting la.pptx
javascript client side scripting la.pptxjavascript client side scripting la.pptx
javascript client side scripting la.pptx
 
Railroading into Scala
Railroading into ScalaRailroading into Scala
Railroading into Scala
 
Pj01 3-java-variable and data types
Pj01 3-java-variable and data typesPj01 3-java-variable and data types
Pj01 3-java-variable and data types
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3
 
Java Script
Java ScriptJava Script
Java Script
 

More from Rajeev Kumar

Db performance optimization with indexing
Db performance optimization with indexingDb performance optimization with indexing
Db performance optimization with indexing
Rajeev Kumar
 
Javasession10
Javasession10Javasession10
Javasession10
Rajeev Kumar
 
Java session2
Java session2Java session2
Java session2
Rajeev Kumar
 

More from Rajeev Kumar (9)

Db performance optimization with indexing
Db performance optimization with indexingDb performance optimization with indexing
Db performance optimization with indexing
 
Javasession10
Javasession10Javasession10
Javasession10
 
Jms intro
Jms introJms intro
Jms intro
 
Javasession8
Javasession8Javasession8
Javasession8
 
Javasession6
Javasession6Javasession6
Javasession6
 
Javasession7
Javasession7Javasession7
Javasession7
 
Javasession5
Javasession5Javasession5
Javasession5
 
Javasession4
Javasession4Javasession4
Javasession4
 
Java session2
Java session2Java session2
Java session2
 

Recently uploaded

Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 

Recently uploaded (20)

Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 

Java session 3

  • 2. Static variables and Methods ❖ ❖ Variables and methods marked as static belongs to class, rather than any particular instance. One copy is shared across all class instances. ➢ ❖ ❖ ❖ ❖ ● Ex : https://gist.github.com/rajeevprasanna/8754235 A static method can’t access a nonstatic(instance) variable, because there is not instance. ex : https://gist.github.com/rajeevprasanna/8754328 Static=class, nonstatic=instance. Access static method or variable by dot operator on className. Static methods can’t be overridden. ex : https://gist.github.com/rajeevprasanna/8754437 Static or initialization block: Initialization blocks run when the class is first loaded (a static initialization block) or when an instance is created. ex : https://gist.github.com/rajeevprasanna/8756880
  • 3. Data types and primitives Data Types : Primitive types are special data types built into the language; they are not objects created from a class Literal : A Literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation boolean result = true; boolean - is data type true - is literal Refer : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
  • 4. Float and Double ➔ ➔ ➔ A float is a 32 bit IEEE 754 floating point. A double is a 64 bit IEEE 754 floating point. You shouldn't ever compare floats or doubles for equality; because, you can't really guarantee that the number you assign to the float or double is exact. Ex : https://gist.github.com/rajeevprasanna/8754840
  • 5. Character literals ● ● Char literal is represented by a single character in single quotes It stores the 16-bit Unicode integer value of the character in question ■ char a = 'a'; ■ char letterN = 'u004E'; // The letter 'N' in unicode representation ■ char a = 0x892; // hexadecimal literal ■ char b = 982; // int literal ■ char c = (char)70000; // The cast is required; 70000 is out of char range ■ char d = (char) -98; // Ridiculous, but legal ■ char e = -29; //Error : Possible loss of precision; needs a cast ■ char f = 70000 //Error : Possible loss of precision; needs a cast ● Use an escape code if you want to represent a character that can't be typed in as a literal, including the characters for line feed, newline, horizontal tab, backspace, and single quotes. ■ char c = '"'; // A double quote ■ ● char d = 'n'; // A newline If given character is out of range, it is displayed as ?. Refer table here ○ Ex : https://gist.github.com/rajeevprasanna/8755247
  • 6. Assignment operators ● ● Result of an expression involving anything int-sized or smaller is always an int Multiply an int and a short and you'll get an int. Divide a short by a byte and you'll get...an int. ex : https://gist.github.com/rajeevprasanna/8755470 ● ● ● Assigning Floating-Point Numbers : In order to assign a floating-point literal to a float variable, you must either cast the value or append an f to the end of the literal(compiler treat as double if no casting is there). ○ float f = (float) 32.3 ○ float g = 32.3f ○ float h = 32.3F byte a = 128; // Error : byte can only hold up to 127(Assigning a Literal That Is Too Large for the Variable) Array instance variable initialization : Array elements are always, always, always given default values, regardless of where the array itself is declared or instantiated.
  • 7. Passing variables into methods ❖ ❖ ❖ Passing primitive variables Passing reference variables Does java use Pass-By-Value ? ➢ ❖ ❖ Java is actually pass-by-value for all variables running within a single VM. Pass-by-value means pass-by-variable-value. And that means, pass-by-copy-of- the-variable! ➢ ex : https://gist.github.com/rajeevprasanna/8755774 Shadowing instance/static primitive variables. ➢ ex: https://gist.github.com/rajeevprasanna/8755805 Shadow instance reference variables. ➢ ex : https://gist.github.com/rajeevprasanna/8755845
  • 8. Arrays ❖ ❖ ❖ ❖ Declaring arrays ➢ int[] key;//preferred, int key []; (primitive array declaration) ➢ Thread[] threads; //Declaring array of object references. Array object is created on the heap. You must specify the size of the array at creation time. The size of the array is the number of elements the array will hold. One dimensional array : ➢ int[] testScores; // Declares the array of ints testScores = new int[4]; ❖ ❖ ❖ ➢ int[] testScores = new int[4];//Initialize at the time of declaration. ➢ int[] carList = new int[]; // Will not compile; needs a size Multi dimensional array : ➢ Multidimensional arrays, remember, are simply arrays of arrays. ➢ So a two- dimensional array of type int is really an object of type int array (int []), with each element in that array holding a reference to another int array. ➢ int[][] myArray = new int[3][]; //valid Array initialization: https://gist.github.com/rajeevprasanna/8756729 Array index starts from 0 to arrayLength-1.
  • 9. Wrapper class The wrapper classes in the Java API serve two primary purposes: ● ● ● ● To provide a mechanism to "wrap" primitive values in an object so that the primitives can be included in activities reserved for objects, like being added to Collections, or returned from a method with an object return value. To provide an assortment of utility functions for primitives. Most of these functions are related to various conversions: converting primitives to and from String objects, and converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal. Wrapper objects are immutable. Once they have been given a value, that value cannot be changed. Methods : valueOf, xxxValue, toXxxString,
  • 10. Overloading ● ● ● ● Widening. ex : https://gist.github.com/rajeevprasanna/8757265 Autoboxing Var-args When overloading, compiler will choose widening over boxing. ○ Widening beats boxing ○ Widening beats var-args ○ Boxing beats var-args ○ ● ex : https://gist.github.com/rajeevprasanna/8757252 Widening reference variables. ex: https://gist.github.com/rajeevprasanna/8757479
  • 11. Equality operator ❖ ❖ ❖ ❖ == equals (also known as "equal to") != not equals (also known as "not equal to") Equal operation on primitives, references and enums. ➢ ex: https://gist.github.com/rajeevprasanna/8757682 instanceof Comparison: ➢ ❖ The instanceof operator is used for object reference variables only, and you can use it to check whether an object is of a particular type. ➢ By type, we mean class or interface type—in other words, if the object referred to by the variable on the left side of the operator passes the IS-A test for the class or interface type on the right side ■ ex: https://gist.github.com/rajeevprasanna/8757785 ➢ You can't use the instanceof operator to test across two different class hierarchies. ■ ex: https://gist.github.com/rajeevprasanna/8757802 Remember that arrays are objects, even if the array is an array of primitives ➢ int [] nums = new int[3]; if (nums instanceof Object) { } // result is true