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;