Java
Computer Programming 2
2I
Java is a programming language and environment
invented by James Gosling and others in 1994. Java was
originally named Oak and was developed as a part of the
Green project at the Sun (Stanford University Network)
Company.
History of Java
Is a high-level, third generation programming language,
like C, FORTAN, smaltalk, Perl, and many others. You
can use Java to write computer applications that play
games, store date or do any of the thousands of other
things computer software can do.
Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
Sample Program
Java is:
Object Oriented
Platform independent:
Simple
Secure
Architectural- neutral
Portable
Robust
Multi-threaded
Interpreted
High Performance
Distributed
Dynamic
What is Java:
Object - Objects have states and behaviors. Example: A dog has
states-color, name, breed as well as behaviors -wagging,
barking, eating. An object is an instance of a class.
Class - A class can be defined as a template/ blue print that describe
the behaviors/states that object of its type support.
Methods - A method is basically a behavior. A class can contain
many methods. It is in methods where the logics are written,
data is manipulated and all the actions are executed.
Instant Variables - Each object has its unique set of instant
variables. An object's state is created by the values assigned to
these instant variables.
Java Basic Syntax
Let us now look deep into what are objects. If we consider the realworld we can find many objects around us, Cars, Dogs, Humans, etc.
All these objects have a state and behavior.
If we consider a dog, then its state is - name, breed, color, and the
behavior is - barking, wagging, running
If you compare the software object with a real world object, they
have very similar characteristics.
Software objects also have a state and behavior. A software object's
state is stored in fields and behavior is shown via methods.
So in software development, methods operate on the internal state of
an object and the object-to-object communication is done via
methods.
Object & Classes (Object)
A class is a blue print from which individual objects are
created.
Object & Classes (Class)
public class Dog{
String breed;
int age;
String color;
void barking(){
}
void hungry(){
}
void sleeping(){
}
}
Object & Classes (Class)
identifier
userName
user_name
_sys_var1
$change
Java Identifiers
In java there are several points to remember about identifiers. They
are as follows:
All identifiers should begin with a letter (A to Z or a to z ),
currency character ($) or an underscore (_).
After the first character identifiers can have any combination of
characters.
A key word cannot be used as an identifier.
Most importantly identifiers are case sensitive.
Examples of legal identifiers:age, $salary, _value, __1_value
Examples of illegal identifiers : 123abc, -salary
Java Identifiers
abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
Java Keywords
byte
short
Int
Long
Float
Double
Boolean
char
Java Data Types
Byte data type is an 8-bit signed two's complement integer.
Minimum value is -128 (-2^7)
Maximum value is 127 (inclusive)(2^7 -1)
Default value is 0
Byte data type is used to save space in large arrays, mainly in place
of integers, since a byte is four times smaller than an int.
Example: byte a = 100 , byte b = -50
Byte
Short data type is a 16-bit signed two's complement integer.
Minimum value is -32,768 (-2^15)
Maximum value is 32,767 (inclusive) (2^15 -1)
Short data type can also be used to save memory as byte data type. A
short is 2 times smaller than an int
Default value is 0.
Example: short s = 10000, short r = -20000
Short
Int data type is a 32-bit signed two's complement integer.
Minimum value is - 2,147,483,648.(-2^31)
Maximum value is 2,147,483,647(inclusive).(2^31 -1)
Int is generally used as the default data type for integral values unless
there is a concern about memory.
The default value is 0.
Example: int a = 100000, int b = -200000
int
Long data type is a 64-bit signed two's complement integer.
Minimum value is -9,223,372,036,854,775,808.(-2^63)
Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
This type is used when a wider range than int is needed.
Default value is 0L.
Example: long a = 100000L, int b = -200000L
long
Float data type is a single-precision 32-bit IEEE 754 floating point.
Float is mainly used to save memory in large arrays of floating point
numbers.
Default value is 0.0f.
Float data type is never used for precise values such as currency.
Example: float f1 = 234.5f
float
double data type is a double-precision 64-bit IEEE 754 floating point.
This data type is generally used as the default data type for decimal
values, generally the default choice.
Double data type should never be used for precise values such as
currency.
Default value is 0.0d.
Example: double d1 = 123.4
double
boolean data type represents one bit of information.
There are only two possible values: true and false.
This data type is used for simple flags that track true/false conditions.
Default value is false.
Example: boolean one = true
boolean
char data type is a single 16-bit Unicode character.
Minimum value is '\u0000' (or 0).
Maximum value is '\uffff' (or 65,535 inclusive).
Char data type is used to store any character.
Example: char letterA ='A'
char