SlideShare una empresa de Scribd logo
1 de 27
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;

Más contenido relacionado

La actualidad más candente

Local variables Instance variables Class/static variables
Local variables Instance variables Class/static variablesLocal variables Instance variables Class/static variables
Local variables Instance variables Class/static variablesSohanur63
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm Madishetty Prathibha
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteTushar B Kute
 
Learning core java
Learning core javaLearning core java
Learning core javaAbhay Bharti
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfacesDevaKumari Vijay
 

La actualidad más candente (11)

Lesson3
Lesson3Lesson3
Lesson3
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Oop java
Oop javaOop java
Oop java
 
Local variables Instance variables Class/static variables
Local variables Instance variables Class/static variablesLocal variables Instance variables Class/static variables
Local variables Instance variables Class/static variables
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
Learning core java
Learning core javaLearning core java
Learning core java
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
 
PCSTt11 overview of java
PCSTt11 overview of javaPCSTt11 overview of java
PCSTt11 overview of java
 

Similar a Java

Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)Khaled Anaqwa
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryPray Desai
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itlokeshpappaka10
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to ScalaRahul Jain
 
Enumerations in java.pptx
Enumerations in java.pptxEnumerations in java.pptx
Enumerations in java.pptxSrizan Pokrel
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardHari kiran G
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptxVijalJain3
 
cs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxcs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxmshanajoel6
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentalsAnsgarMary
 
intro_java (1).pptx
intro_java (1).pptxintro_java (1).pptx
intro_java (1).pptxSmitNikumbh
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxethiouniverse
 
chapter 1-overview of java programming.pptx
chapter 1-overview of java programming.pptxchapter 1-overview of java programming.pptx
chapter 1-overview of java programming.pptxnafsigenet
 

Similar a Java (20)

Java
JavaJava
Java
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Javasession6
Javasession6Javasession6
Javasession6
 
Apex code (Salesforce)
Apex code (Salesforce)Apex code (Salesforce)
Apex code (Salesforce)
 
Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
 
Enumerations in java.pptx
Enumerations in java.pptxEnumerations in java.pptx
Enumerations in java.pptx
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 
cs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxcs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptx
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
 
intro_java (1).pptx
intro_java (1).pptxintro_java (1).pptx
intro_java (1).pptx
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Module 1.pptx
Module 1.pptxModule 1.pptx
Module 1.pptx
 
chapter 1-overview of java programming.pptx
chapter 1-overview of java programming.pptxchapter 1-overview of java programming.pptx
chapter 1-overview of java programming.pptx
 

Más de Raghu nath

Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)Raghu nath
 
Javascript part1
Javascript part1Javascript part1
Javascript part1Raghu nath
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Selection sort
Selection sortSelection sort
Selection sortRaghu nath
 
Binary search
Binary search Binary search
Binary search Raghu nath
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)Raghu nath
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithmsRaghu nath
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp roleRaghu nath
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4Raghu nath
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3Raghu nath
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2Raghu nath
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2Raghu nath
 
python chapter 1
python chapter 1python chapter 1
python chapter 1Raghu nath
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell ScriptingRaghu nath
 

Más de Raghu nath (20)

Mongo db
Mongo dbMongo db
Mongo db
 
Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)
 
MS WORD 2013
MS WORD 2013MS WORD 2013
MS WORD 2013
 
Msword
MswordMsword
Msword
 
Ms word
Ms wordMs word
Ms word
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Selection sort
Selection sortSelection sort
Selection sort
 
Binary search
Binary search Binary search
Binary search
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithms
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp role
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Perl
PerlPerl
Perl
 

Último

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Último (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

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;