SlideShare una empresa de Scribd logo
1 de 11
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
Vasil Remeniuk
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)
mircodotta
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3
Chris Farrell
 

La actualidad más candente (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
 

Destacado (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 a Java session 3

Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
Chikugehlot
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 

Similar a 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
 

Más de 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
 

Último

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 

Último (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 

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