SlideShare a Scribd company logo
Java
What Is Java
Java is an object-oriented programming language developed by Sun
Microsystems, a company best known for its high-end Unix workstations.
Modeled after C++, the Java language was designed to be small, simple, and
portable across platforms and operating systems
• Java Is Platform-Independent
• Java Is Easy to Learn
Your first Java application
class HelloWorld {
public static void main (String args[]) {
System.out.println(“Hello World!”);
}
Object-Oriented Programming and Java
• Objects and Classes
• Behavior and Attributes
• Attributes
• Behavior
• Creating a Class
Inheritance, Interfaces, and Packages
• Inheritance
Inheritance is one of the most crucial concepts in object-oriented
programming, and it has a very direct effect on how you design and write your
Java classes. Inheritance is a powerful mechanism that means when you write a
class you only have to specify how that class is different from some other class,
while also giving you dynamic access to the information contained in those
other classes.
Single and Multiple Inheritance
Java’s form of inheritance, as you learned in the previous sections, is called single inheritance.
Single inheritance means that each Java class can have only one superclass (although any give
superclass can have multiple subclasses). In other object-oriented programming languages, such
as C++ and Smalltalk, classes can have more than one superclass, and they inherit combined
variables and methods from all those classes. This is called multiple inheritance. Multiple
inheritance can provide enormous power in terms of being able to create classes that factor
just about all imaginable behavior, but it can also significantly complicate class definitions and
the code to produce them. Java makes inheritance simpler by being only singly inherited.
Interfaces and Packages
• An interface is a collection of method names, without actual definitions, that
indicate that a class has a set of behaviors in addition to the behaviors the
class gets from its superclasses.
• Packages in Java are a way of grouping together related classes and interfaces.
Packages enable modular groups of classes to be available only if they are
needed and eliminate potential conflicts between class names in different
groups of classes.
Important Points:
• Class: A template for an object, which contains variables and methods representing behavior and attributes. Classes can
inherit variables and methods from other classes. Object: A concrete instance of some class. Multiple objects that are
instances of the same class have access to the same methods, but often have different values for their instance variables.
Instance: The same thing as an object; each object is an instance of some class.
• Superclass: A class further up in the inheritance hierarchy than its child, the subclass. Subclass: A class lower in the
inheritance hierarchy than its parent, the superclass. When you create a new class, that’s often called subclassing.
• Instance method: A method defined in a class, which operates on an instance of that class. Instance methods are usually
called just methods. Class method: A method defined in a class, which can operate on the class itself or on any object.
• Instance variable: A variable that is owned by an individual instance and whose value is stored in the instance.
• Class variable: A variable that is owned by the class and all its instances as a whole, and is stored in the class.
• Interface: A collection of abstract behavior specifications that individual classes can then implement.
• Package: A collection of classes and interfaces. Classes from packages other than java.lang must be explicitly imported or
referred to by full package name.
Statements and Expressions
A statement is the simplest thing you can do in Java; a statement forms a single Java
operation. All the following are simple Java statements:
int i = 1;
import java.awt.Font;
System.out.println(“This motorcycle is a “ + color + “ “ + make);
m.engineState = true;
Statements sometimes return values—for example, when you add two numbers
together or test to see whether one value is equal to another. These kind of statements
are called expressions. We’ll discuss these later on today
Variables and Data Types
• Java actually has three kinds of variables: instance variables, class variables,
and local variables
Declaring Variables
• To use any variable in a Java program, you must first declare it. Variable
declarations consist of a type and a variable name:
• int myAge;
• String myName;
• boolean isTired;
• Variable definitions can go anywhere in a method definition (that is, anywhere a regular Java statement can
go), although they are most commonly declared at the beginning of the definition before they are used:
• public static void main (String args÷]) {
• int count;
• String title;
• boolean isAsleep;
• ...
• }
Notes on Variable Names
• Variable names in Java can start with a letter, an underscore (_), or a dollar
sign ($). They cannot start with a number. After the first character, your
variable names can include any letter or number. Symbols, such as %, *, @,
and so on, are often reserved for operators in Java, so be careful when using
symbols in variable names.
• Type Size Range
• Byte 8 bits –128 to 127
• short 16 bits –-32,768 to 32,767
• int 32 bits –2,147,483,648 to 2,147,483,647
• long 64 bits –9223372036854775808 to 9223372036854775807
Assigning Values to Variables
• Once a variable has been declared, you can assign a value to that variable by
using the assignment
• operator =:
• size = 14;
• tooMuchCaffiene = true;
Comments
• Java has three kinds of comments. /* and */ surround multiline comments, as in C or C++. All
text between the two delimiters is ignored: /* I don’t know how I wrote this next part; I was
working really late one night and it just sort of appeared. I suspect the code elves did it for me.
It might be wise not to try and change it. */ Comments cannot be nested; that is, you cannot
have a comment inside a comment.
• Double-slashes (//) can be used for a single line of comment. All the text up to the end of the
• line is ignored: int vices = 7; // are there really only 7 vices?
• The final type of comment begins with /** and ends with */. These are special comments that
are used for the javadoc system. Javadoc is used to generate API documentation from the code.
You won’t learn about javadoc in this book; you can find out more information from the
documentation that came with Sun’s Java Developer’s Kit or from Sun’s Java home page (http:/
/java.sun.com).
Literals
Literal is a programming language term, which essentially means that what you type is what you get
Number Literals
There are several integer literals. 4, for example, is a decimal integer literal of type int (although
you can assign it to a variable of type byte or short because it’s small enough to fit into those
types). A decimal integer literal larger than an int is automatically of type long. You also can force
a smaller number to a long by appending an L or l to that number (for example, 4L is a long
integer of value 4). Negative integers are preceded by a minus sign—for example, -45.
Test of prefix and postfix increment operators
• Comparisons
• Operator Meaning Example
• == Equal x == 3
• != Not equal x != 3
• < Less than x < 3
• > Greater than x > 3
• ≤ Less than or equal to x ≤ 3
• ≥ Greater than or equal to x ≥ 3
Logical Operators
• Expressions that result in boolean values (for example, the comparison operators) can be combined by using
logical operators that represent the logical combinations AND, OR, XOR, and logical NOT.
• Bitwise Operators
• Operator Meaning
• & Bitwise AND
• | Bitwise OR
• ^ Bitwise XOR
• << Left shift
• >> Right shift
• >>> Zero fill right shift
• Operator Meaning
• ~ Bitwise complement
• <<= Left shift assignment (x = x << y)
• >>= Right shift assignment (x = x >> y)
• >>>= Zero fill right shift assignment (x = x >>> y)
• x&=y AND assignment (x = x & y)
• x|=y OR assignment (x + x | y)
• x^=y NOT assignment (x = x ^ y
Operator Precedence
• Operator precedence determines the order in which expressions are
evaluated. This, in some cases, can determine the overall value of the
expression. For example, take the following expression:
• y = 6 + 4 / 2
String Arithmetic
• One special expression in Java is the use of the addition operator (+) to
create and concatenate strings. In most of the previous examples shown
today and in earlier lessons, you’ve seen lots of lines that looked something
like this:
• System.out.println(name + “ is a “ + color “ beetle”);
Working with
Objects
• Creating New Objects
• To create a new object, you use new with the name of the class you want to
create an instance of, then parentheses after that:
• String str = new String();
• Random r = new Random();
• Motorcycle m2 = new Motorcycle()
What new Does
• Constructors are special methods for creating and initializing new instances
of classes. Constructors initialize the new object and its variables, create any
other objects that object needs, and generally perform any other operations
the object needs to run.
Memory Management
• Memory management in Java is dynamic and automatic. When you create a
new object in Java, Java automatically allocates the right amount of memory
for that object in the heap
Accessing and Setting Class and
Instance Variables
• Getting Values
• With dot notation, an instance or class variable name has two parts: the object on the left side of
the dot, and the variable on the right side of the dot.
• For example, if you have an object assigned to the variable myObject, and that object has a
variable called var, you refer to that variable’s value like this:
• myObject.var;
• This form for accessing variables is an expression (it returns a value), and both sides of the dot
are also expressions. This means that you can nest instance variable access. If that var instance
variable itself holds an object, and that object has its own instance variable called state, you can
refer to it like this:
• myObject.var.state;

More Related Content

What's hot (11)

PPT
Lesson3
Arpan91
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPT
Oop java
Minal Maniar
 
PPTX
Local variables Instance variables Class/static variables
Sohanur63
 
PDF
Metaprogramming ruby
GeekNightHyderabad
 
PDF
Java data types, variables and jvm
Madishetty Prathibha
 
PDF
Chapter 01 Introduction to Java by Tushar B Kute
Tushar B Kute
 
PPTX
Learning core java
Abhay Bharti
 
PDF
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
PPTX
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
PPTX
PCSTt11 overview of java
Archana Gopinath
 
Lesson3
Arpan91
 
Classes objects in java
Madishetty Prathibha
 
Oop java
Minal Maniar
 
Local variables Instance variables Class/static variables
Sohanur63
 
Metaprogramming ruby
GeekNightHyderabad
 
Java data types, variables and jvm
Madishetty Prathibha
 
Chapter 01 Introduction to Java by Tushar B Kute
Tushar B Kute
 
Learning core java
Abhay Bharti
 
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
PCSTt11 overview of java
Archana Gopinath
 

Similar to Java (20)

PPT
Java Basics
Brandon Black
 
PPT
Java_notes.ppt
tuyambazejeanclaude
 
PPTX
Java Basics 1.pptx
TouseeqHaider11
 
PPT
java01.pptbvuyvyuvvvvvvvvvvvvvvvvvvvvyft
nagendrareddy104104
 
PPT
Java Simple Introduction in single course
binzbinz3
 
PPT
Java
Manav Prasad
 
PPTX
Module 1.pptx
YakaviBalakrishnan
 
PPTX
Introduction to java Programming Language
rubyjeyamani1
 
ODP
Introduction To Java.
Tushar Chauhan
 
PPT
Core java day1
Soham Sengupta
 
PPTX
Java programmingjsjdjdjdjdjdjdjdjdiidiei
rajputtejaswa12
 
PPTX
Java Jive 002.pptx
AdarshSingh202130
 
PDF
java
Kunal Sunesara
 
PPT
Present the syntax of Java Introduce the Java
ssuserfd620b
 
PPT
java ppt for basic intro about java and its
kssandhu875
 
PPT
Java - A parent language and powerdul for mobile apps.
dhimananshu130803
 
PPT
Java intro
Sonam Sharma
 
PPT
Java01
Remon Hanna
 
PPT
Java01
Dhaval Patel
 
Java Basics
Brandon Black
 
Java_notes.ppt
tuyambazejeanclaude
 
Java Basics 1.pptx
TouseeqHaider11
 
java01.pptbvuyvyuvvvvvvvvvvvvvvvvvvvvyft
nagendrareddy104104
 
Java Simple Introduction in single course
binzbinz3
 
Module 1.pptx
YakaviBalakrishnan
 
Introduction to java Programming Language
rubyjeyamani1
 
Introduction To Java.
Tushar Chauhan
 
Core java day1
Soham Sengupta
 
Java programmingjsjdjdjdjdjdjdjdjdiidiei
rajputtejaswa12
 
Java Jive 002.pptx
AdarshSingh202130
 
Present the syntax of Java Introduce the Java
ssuserfd620b
 
java ppt for basic intro about java and its
kssandhu875
 
Java - A parent language and powerdul for mobile apps.
dhimananshu130803
 
Java intro
Sonam Sharma
 
Java01
Remon Hanna
 
Java01
Dhaval Patel
 
Ad

More from Raghu nath (20)

PPTX
Mongo db
Raghu nath
 
PDF
Ftp (file transfer protocol)
Raghu nath
 
PDF
MS WORD 2013
Raghu nath
 
PDF
Msword
Raghu nath
 
PDF
Ms word
Raghu nath
 
PDF
Javascript part1
Raghu nath
 
PDF
Regular expressions
Raghu nath
 
PDF
Selection sort
Raghu nath
 
PPTX
Binary search
Raghu nath
 
PPTX
JSON(JavaScript Object Notation)
Raghu nath
 
PDF
Stemming algorithms
Raghu nath
 
PPTX
Step by step guide to install dhcp role
Raghu nath
 
PPTX
Network essentials chapter 4
Raghu nath
 
PPTX
Network essentials chapter 3
Raghu nath
 
PPTX
Network essentials chapter 2
Raghu nath
 
PPTX
Network essentials - chapter 1
Raghu nath
 
PPTX
Python chapter 2
Raghu nath
 
PPTX
python chapter 1
Raghu nath
 
PPTX
Linux Shell Scripting
Raghu nath
 
PPTX
Perl
Raghu nath
 
Mongo db
Raghu nath
 
Ftp (file transfer protocol)
Raghu nath
 
MS WORD 2013
Raghu nath
 
Msword
Raghu nath
 
Ms word
Raghu nath
 
Javascript part1
Raghu nath
 
Regular expressions
Raghu nath
 
Selection sort
Raghu nath
 
Binary search
Raghu nath
 
JSON(JavaScript Object Notation)
Raghu nath
 
Stemming algorithms
Raghu nath
 
Step by step guide to install dhcp role
Raghu nath
 
Network essentials chapter 4
Raghu nath
 
Network essentials chapter 3
Raghu nath
 
Network essentials chapter 2
Raghu nath
 
Network essentials - chapter 1
Raghu nath
 
Python chapter 2
Raghu nath
 
python chapter 1
Raghu nath
 
Linux Shell Scripting
Raghu nath
 
Ad

Recently uploaded (20)

PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
community health nursing question paper 2.pdf
Prince kumar
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 

Java

  • 2. What Is Java Java is an object-oriented programming language developed by Sun Microsystems, a company best known for its high-end Unix workstations. Modeled after C++, the Java language was designed to be small, simple, and portable across platforms and operating systems
  • 3. • Java Is Platform-Independent • Java Is Easy to Learn
  • 4. Your first Java application class HelloWorld { public static void main (String args[]) { System.out.println(“Hello World!”); }
  • 5. Object-Oriented Programming and Java • Objects and Classes • Behavior and Attributes • Attributes • Behavior • Creating a Class
  • 6. Inheritance, Interfaces, and Packages • Inheritance Inheritance is one of the most crucial concepts in object-oriented programming, and it has a very direct effect on how you design and write your Java classes. Inheritance is a powerful mechanism that means when you write a class you only have to specify how that class is different from some other class, while also giving you dynamic access to the information contained in those other classes.
  • 7. Single and Multiple Inheritance Java’s form of inheritance, as you learned in the previous sections, is called single inheritance. Single inheritance means that each Java class can have only one superclass (although any give superclass can have multiple subclasses). In other object-oriented programming languages, such as C++ and Smalltalk, classes can have more than one superclass, and they inherit combined variables and methods from all those classes. This is called multiple inheritance. Multiple inheritance can provide enormous power in terms of being able to create classes that factor just about all imaginable behavior, but it can also significantly complicate class definitions and the code to produce them. Java makes inheritance simpler by being only singly inherited.
  • 8. Interfaces and Packages • An interface is a collection of method names, without actual definitions, that indicate that a class has a set of behaviors in addition to the behaviors the class gets from its superclasses. • Packages in Java are a way of grouping together related classes and interfaces. Packages enable modular groups of classes to be available only if they are needed and eliminate potential conflicts between class names in different groups of classes.
  • 9. Important Points: • Class: A template for an object, which contains variables and methods representing behavior and attributes. Classes can inherit variables and methods from other classes. Object: A concrete instance of some class. Multiple objects that are instances of the same class have access to the same methods, but often have different values for their instance variables. Instance: The same thing as an object; each object is an instance of some class. • Superclass: A class further up in the inheritance hierarchy than its child, the subclass. Subclass: A class lower in the inheritance hierarchy than its parent, the superclass. When you create a new class, that’s often called subclassing. • Instance method: A method defined in a class, which operates on an instance of that class. Instance methods are usually called just methods. Class method: A method defined in a class, which can operate on the class itself or on any object. • Instance variable: A variable that is owned by an individual instance and whose value is stored in the instance. • Class variable: A variable that is owned by the class and all its instances as a whole, and is stored in the class. • Interface: A collection of abstract behavior specifications that individual classes can then implement. • Package: A collection of classes and interfaces. Classes from packages other than java.lang must be explicitly imported or referred to by full package name.
  • 10. Statements and Expressions A statement is the simplest thing you can do in Java; a statement forms a single Java operation. All the following are simple Java statements: int i = 1; import java.awt.Font; System.out.println(“This motorcycle is a “ + color + “ “ + make); m.engineState = true; Statements sometimes return values—for example, when you add two numbers together or test to see whether one value is equal to another. These kind of statements are called expressions. We’ll discuss these later on today
  • 11. Variables and Data Types • Java actually has three kinds of variables: instance variables, class variables, and local variables
  • 12. Declaring Variables • To use any variable in a Java program, you must first declare it. Variable declarations consist of a type and a variable name: • int myAge; • String myName; • boolean isTired;
  • 13. • Variable definitions can go anywhere in a method definition (that is, anywhere a regular Java statement can go), although they are most commonly declared at the beginning of the definition before they are used: • public static void main (String args÷]) { • int count; • String title; • boolean isAsleep; • ... • }
  • 14. Notes on Variable Names • Variable names in Java can start with a letter, an underscore (_), or a dollar sign ($). They cannot start with a number. After the first character, your variable names can include any letter or number. Symbols, such as %, *, @, and so on, are often reserved for operators in Java, so be careful when using symbols in variable names.
  • 15. • Type Size Range • Byte 8 bits –128 to 127 • short 16 bits –-32,768 to 32,767 • int 32 bits –2,147,483,648 to 2,147,483,647 • long 64 bits –9223372036854775808 to 9223372036854775807
  • 16. Assigning Values to Variables • Once a variable has been declared, you can assign a value to that variable by using the assignment • operator =: • size = 14; • tooMuchCaffiene = true;
  • 17. Comments • Java has three kinds of comments. /* and */ surround multiline comments, as in C or C++. All text between the two delimiters is ignored: /* I don’t know how I wrote this next part; I was working really late one night and it just sort of appeared. I suspect the code elves did it for me. It might be wise not to try and change it. */ Comments cannot be nested; that is, you cannot have a comment inside a comment. • Double-slashes (//) can be used for a single line of comment. All the text up to the end of the • line is ignored: int vices = 7; // are there really only 7 vices? • The final type of comment begins with /** and ends with */. These are special comments that are used for the javadoc system. Javadoc is used to generate API documentation from the code. You won’t learn about javadoc in this book; you can find out more information from the documentation that came with Sun’s Java Developer’s Kit or from Sun’s Java home page (http:/ /java.sun.com).
  • 18. Literals Literal is a programming language term, which essentially means that what you type is what you get Number Literals There are several integer literals. 4, for example, is a decimal integer literal of type int (although you can assign it to a variable of type byte or short because it’s small enough to fit into those types). A decimal integer literal larger than an int is automatically of type long. You also can force a smaller number to a long by appending an L or l to that number (for example, 4L is a long integer of value 4). Negative integers are preceded by a minus sign—for example, -45.
  • 19. Test of prefix and postfix increment operators • Comparisons • Operator Meaning Example • == Equal x == 3 • != Not equal x != 3 • < Less than x < 3 • > Greater than x > 3 • ≤ Less than or equal to x ≤ 3 • ≥ Greater than or equal to x ≥ 3
  • 20. Logical Operators • Expressions that result in boolean values (for example, the comparison operators) can be combined by using logical operators that represent the logical combinations AND, OR, XOR, and logical NOT. • Bitwise Operators • Operator Meaning • & Bitwise AND • | Bitwise OR • ^ Bitwise XOR • << Left shift • >> Right shift • >>> Zero fill right shift
  • 21. • Operator Meaning • ~ Bitwise complement • <<= Left shift assignment (x = x << y) • >>= Right shift assignment (x = x >> y) • >>>= Zero fill right shift assignment (x = x >>> y) • x&=y AND assignment (x = x & y) • x|=y OR assignment (x + x | y) • x^=y NOT assignment (x = x ^ y
  • 22. Operator Precedence • Operator precedence determines the order in which expressions are evaluated. This, in some cases, can determine the overall value of the expression. For example, take the following expression: • y = 6 + 4 / 2
  • 23. String Arithmetic • One special expression in Java is the use of the addition operator (+) to create and concatenate strings. In most of the previous examples shown today and in earlier lessons, you’ve seen lots of lines that looked something like this: • System.out.println(name + “ is a “ + color “ beetle”);
  • 24. Working with Objects • Creating New Objects • To create a new object, you use new with the name of the class you want to create an instance of, then parentheses after that: • String str = new String(); • Random r = new Random(); • Motorcycle m2 = new Motorcycle()
  • 25. What new Does • Constructors are special methods for creating and initializing new instances of classes. Constructors initialize the new object and its variables, create any other objects that object needs, and generally perform any other operations the object needs to run.
  • 26. Memory Management • Memory management in Java is dynamic and automatic. When you create a new object in Java, Java automatically allocates the right amount of memory for that object in the heap
  • 27. Accessing and Setting Class and Instance Variables • Getting Values • With dot notation, an instance or class variable name has two parts: the object on the left side of the dot, and the variable on the right side of the dot. • For example, if you have an object assigned to the variable myObject, and that object has a variable called var, you refer to that variable’s value like this: • myObject.var; • This form for accessing variables is an expression (it returns a value), and both sides of the dot are also expressions. This means that you can nest instance variable access. If that var instance variable itself holds an object, and that object has its own instance variable called state, you can refer to it like this: • myObject.var.state;