SlideShare a Scribd company logo
Java references and parameters passing Truth #1: The values of variables in Java are always primitives or references, never objects.   public static void foo(String bar) {  Integer baz = new Integer(bar);  }
Java references and parameters passing Truth #2: Everything in Java is passed by value. Objects, however, are never passed at all.   1.   public void  foo(Dog d) { 2.     d.name == "Max"; // true 3.    d = new Dog("Fifi"); 4.    d.name == "Fifi"; // true 5.   } 6. 7.   public   static   void  main(String [] args){ 9.   Dog aDog = new Dog("Max"); 10.  foo(aDog); 11.  aDog.name == "Max"; // true 12.  }
Java references and parameters passing Java references vs. C++ pointers Pointers in C++ must be referenced and dereferenced using the *, whereas the referencing and dereferencing of objects is handled for you automatically by Java.  C++ int *num = new int(1); std::cout << num; //result 00345948 Java Integer num =  new  Integer(1); System. out .print(num); //result 1
Java references and parameters passing Java references vs. C++ pointers Java does not allow you to do pointer arithmetic .  C++ class  Number   { public: int  num; Number( int  n um )   { this -> num = n um ; } }; void main (){ Number * ptr  =  new  Number(1); // ptr==00345948 std::cout << (*(++ ptr )).num; // ptr==0034594C delete   ptr ;  //explicitly heap cleaning } //The value of ‘ (*(++ ptr )).num ’ is address with //an uninitialized value(-33686019).We got a Bug. Java public   class  Number { public   int  num; public  Number( int  num) { this .num = num; } } public   class  Main { public   static   void  main(String[] args) { Number ref =  new  Number(1); System. out .print(++(ref.num)); } } //Result is 2
Some Java Basics ‘ final’ keyword A  final class  cannot be extended. This is done for reasons of security and efficiency and  all methods in a final class are implicitly final.  Many of the Java standard library classes are final ,  for example  java.lang.System   and   java.lang.String .   A  final method  cannot be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.  A  final variable  can only be initialized once, either via an initializer or an assignment statement. It need not be initialized at the point of definition: this is called a 'blank final' variable.
Some Java Basics The three forms of Polymorphism Instance methods and overriding An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass  overrides  the superclass's method.  Overloading Overloaded methods are methods with the same  name signature  but either a different number of parameters or different types in the parameter list.    Dynamic (or late) method binding   Dynamic (or late) method binding is the ability of a program to resolve references to subclass methods at  runtime . 
Some Java Basics The three forms of Polymorphism Dynamic (or late) method binding - Example public   abstract   class  Employee { abstract   void  printPosition(); } public   class  Slave  extends  Employee { public   void  printPosition() { System. out .println(&quot;I am the Slave!&quot;); } } public   class  Manager  extends  Employee { public   void  printPosition() { System. out .println(&quot;I am the Boss!&quot;); } } public   class  Main { public   static   void  main(String[] args) { Employee ref; Slave aSlave = new Slave(); ref = aSlave; ref.printPosition();//I am the Slave! Manager aManager = new Manager(); ref = aManager; ref.printPosition();//I am the Boss! } }
Some Java Basics Variables Instance Variables (Non-Static Fields)   Non-static fields are also known as  instance variables  because their values are unique to each  instance  of a class (to each object, in other words).  Class Variables (Static Fields)  A  class variable  is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.  Local Variables  Similar to how an object stores its state in fields, a method will often store its temporary state in  local variables . These are only visible to the methods in which they are declared. Parameters  are variables that are passed to the methods.
Some Java Basics Constants The  static  modifier, in combination with the  final  modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.  String Class The  String  class represents character strings. All string literals in Java programs, such as &quot;abc&quot;, are implemented as instances of this class.  Since strings are instances of the String class, max size of a String class object could be the  available heap memory.
Some Java Basics ‘ final’ keyword A  final class  cannot be extended. This is done for reasons of security and efficiency and  all methods in a final class are implicitly final.  Many of the Java standard library classes are final ,  for example  java.lang.System   and   java.lang.String .   A  final method  cannot be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.  A  final variable  can only be initialized once, either via an initializer or an assignment statement. It need not be initialized at the point of definition: this is called a 'blank final' variable.

More Related Content

What's hot

Polymorphism
PolymorphismPolymorphism
Polymorphism
Prof .Pragati Khade
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
Operators in java
Operators in javaOperators in java
Operators in java
Madishetty Prathibha
 
Java Basics
Java BasicsJava Basics
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
İbrahim Kürce
 
Template Method Pattern
Template Method PatternTemplate Method Pattern
Template Method Pattern
monisiqbal
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
Ram132
 
Java 8
Java 8Java 8
Intro To Scala
Intro To ScalaIntro To Scala
Intro To Scala
chambeda
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
İbrahim Kürce
 
Java moderno java para Jedis episodio I
Java moderno  java para  Jedis  episodio IJava moderno  java para  Jedis  episodio I
Java moderno java para Jedis episodio I
Roan Brasil Monteiro
 
Apply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report ImplementationApply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report ImplementationGuo Albert
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
Manav Prasad
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
Talha Ocakçı
 
Template Method Design Pattern
Template Method Design PatternTemplate Method Design Pattern
Template Method Design Pattern
Srikanth Shreenivas
 
Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
icarter09
 

What's hot (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Java Basics
Java BasicsJava Basics
Java Basics
 
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
 
Template Method Pattern
Template Method PatternTemplate Method Pattern
Template Method Pattern
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Java 8
Java 8Java 8
Java 8
 
Intro To Scala
Intro To ScalaIntro To Scala
Intro To Scala
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
 
Java moderno java para Jedis episodio I
Java moderno  java para  Jedis  episodio IJava moderno  java para  Jedis  episodio I
Java moderno java para Jedis episodio I
 
Apply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report ImplementationApply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report Implementation
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Template Method Design Pattern
Template Method Design PatternTemplate Method Design Pattern
Template Method Design Pattern
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 

Similar to Java findamentals2

Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. NagpurCore & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
PSK Technolgies Pvt. Ltd. IT Company Nagpur
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class featuresRakesh Madugula
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
Gousalya Ramachandran
 
Core java
Core javaCore java
Core java
kasaragaddaslide
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2Raghu nath
 
Java q ref 2018
Java q ref 2018Java q ref 2018
Java q ref 2018
Christopher Akinlade
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
Abhijeet Dubey
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
Learn java
Learn javaLearn java
Learn javaPalahuja
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
Sherihan Anver
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
rajkamaltibacademy
 
java intro.pptx.pdf
java intro.pptx.pdfjava intro.pptx.pdf
java intro.pptx.pdf
TekobashiCarlo
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 
Java 8 lambdas expressions
Java 8 lambdas expressionsJava 8 lambdas expressions
Java 8 lambdas expressions
Lars Lemos
 

Similar to Java findamentals2 (20)

Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. NagpurCore & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
Core java
Core javaCore java
Core java
 
M C6java3
M C6java3M C6java3
M C6java3
 
Java
JavaJava
Java
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2
 
Java q ref 2018
Java q ref 2018Java q ref 2018
Java q ref 2018
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
Learn java
Learn javaLearn java
Learn java
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
java intro.pptx.pdf
java intro.pptx.pdfjava intro.pptx.pdf
java intro.pptx.pdf
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
 
Java 8 lambdas expressions
Java 8 lambdas expressionsJava 8 lambdas expressions
Java 8 lambdas expressions
 
Md04 flow control
Md04 flow controlMd04 flow control
Md04 flow control
 
Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
 

Java findamentals2

  • 1. Java references and parameters passing Truth #1: The values of variables in Java are always primitives or references, never objects. public static void foo(String bar) { Integer baz = new Integer(bar); }
  • 2. Java references and parameters passing Truth #2: Everything in Java is passed by value. Objects, however, are never passed at all. 1. public void foo(Dog d) { 2.   d.name == &quot;Max&quot;; // true 3.   d = new Dog(&quot;Fifi&quot;); 4.   d.name == &quot;Fifi&quot;; // true 5. } 6. 7. public static void main(String [] args){ 9. Dog aDog = new Dog(&quot;Max&quot;); 10. foo(aDog); 11. aDog.name == &quot;Max&quot;; // true 12. }
  • 3. Java references and parameters passing Java references vs. C++ pointers Pointers in C++ must be referenced and dereferenced using the *, whereas the referencing and dereferencing of objects is handled for you automatically by Java. C++ int *num = new int(1); std::cout << num; //result 00345948 Java Integer num = new Integer(1); System. out .print(num); //result 1
  • 4. Java references and parameters passing Java references vs. C++ pointers Java does not allow you to do pointer arithmetic . C++ class Number { public: int num; Number( int n um ) { this -> num = n um ; } }; void main (){ Number * ptr = new Number(1); // ptr==00345948 std::cout << (*(++ ptr )).num; // ptr==0034594C delete ptr ; //explicitly heap cleaning } //The value of ‘ (*(++ ptr )).num ’ is address with //an uninitialized value(-33686019).We got a Bug. Java public class Number { public int num; public Number( int num) { this .num = num; } } public class Main { public static void main(String[] args) { Number ref = new Number(1); System. out .print(++(ref.num)); } } //Result is 2
  • 5. Some Java Basics ‘ final’ keyword A  final class  cannot be extended. This is done for reasons of security and efficiency and all methods in a final class are implicitly final. Many of the Java standard library classes are final , for example java.lang.System and java.lang.String . A  final method  cannot be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class. A final variable  can only be initialized once, either via an initializer or an assignment statement. It need not be initialized at the point of definition: this is called a 'blank final' variable.
  • 6. Some Java Basics The three forms of Polymorphism Instance methods and overriding An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass  overrides  the superclass's method. Overloading Overloaded methods are methods with the same  name signature  but either a different number of parameters or different types in the parameter list.  Dynamic (or late) method binding Dynamic (or late) method binding is the ability of a program to resolve references to subclass methods at  runtime . 
  • 7. Some Java Basics The three forms of Polymorphism Dynamic (or late) method binding - Example public abstract class Employee { abstract void printPosition(); } public class Slave extends Employee { public void printPosition() { System. out .println(&quot;I am the Slave!&quot;); } } public class Manager extends Employee { public void printPosition() { System. out .println(&quot;I am the Boss!&quot;); } } public class Main { public static void main(String[] args) { Employee ref; Slave aSlave = new Slave(); ref = aSlave; ref.printPosition();//I am the Slave! Manager aManager = new Manager(); ref = aManager; ref.printPosition();//I am the Boss! } }
  • 8. Some Java Basics Variables Instance Variables (Non-Static Fields) Non-static fields are also known as  instance variables  because their values are unique to each  instance  of a class (to each object, in other words). Class Variables (Static Fields)  A  class variable  is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. Local Variables  Similar to how an object stores its state in fields, a method will often store its temporary state in  local variables . These are only visible to the methods in which they are declared. Parameters are variables that are passed to the methods.
  • 9. Some Java Basics Constants The  static  modifier, in combination with the  final  modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change. String Class The  String  class represents character strings. All string literals in Java programs, such as &quot;abc&quot;, are implemented as instances of this class. Since strings are instances of the String class, max size of a String class object could be the available heap memory.
  • 10. Some Java Basics ‘ final’ keyword A  final class  cannot be extended. This is done for reasons of security and efficiency and all methods in a final class are implicitly final. Many of the Java standard library classes are final , for example java.lang.System and java.lang.String . A  final method  cannot be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class. A final variable  can only be initialized once, either via an initializer or an assignment statement. It need not be initialized at the point of definition: this is called a 'blank final' variable.