You are on page 1of 6

As a language that has the Object Oriented • Class Names - For all class names the first

feature, Java supports the following letter should be in Upper Case.


fundamental concepts:
• If several words are used to form a name of
• Polymorphism the class, each inner word's first letter should
be in Upper Case. Example class
• Inheritance MyFirstJavaClass
• Encapsulation • Method Names - All method names should
• Abstraction start with a Lower Case letter. If several words
are used to form the name of the method, then
• Classes each inner word's first letter should be in Upper
Case.
• Objects
Example public void myMethodName()
• Instance
• Program File Name - Name of the program
• Method
file should exactly match the class name. When
• Message Parsing saving the file, you should save it using the class
name (Remember Java is case sensitive) and
Object - Objects have states and behaviors. append '.java' to the end of the name (if the file
name and the class name do not match, your
Example: A dog has states - color, name, breed
program will not compile).
as well as behaviors -wagging, barking, eating.
An object is an instance of a class. Example : Assume 'MyFirstJavaProgram' is the
class name. Then the file should be saved as
Class - A class can be defined as a template/
'MyFirstJavaProgram.java'
blue print that describes the behaviors/states
that object of its type support. • public static void main(String args[]) - Java
program processing starts from the main()
Methods - A method is basically a behavior. A
method which is a mandatory part of every Java
class can contain many methods. It is in
program..
methods where the logics are written, data is
manipulated and all the actions are executed.

Instance Variables - Each object has its unique


set of instance variables. An object's state is
created by the values assigned to these instance
variables.

Basic Syntax

• Case Sensitivity - Java is case sensitive, which


means identifier Hello and hello would have
different meaning in Java.
Java Identifier private protected public

Identifiers are user-defined names for return short static


methods, variables, constants, and classes. In
creating identifiers, follow these rules: strictfp super switch synchronized

1. The first character of your identifier should this throw throws


start with a letter of the alphabet, an transient try void
underscore (_), or a dollar sign ($). After that,
the identifier can then be composed of volatile while
alphanumeric characters and underscores.
What is a Data Type?
2. Create identifiers that are descriptive of their
• Is the one who holds variables
purpose. If your identifier is composed of
several words (65,535 characters is the • Java has two sets of data types: primitive and
maximum length per identifier), capitalizing the reference (or non-primitive). Below is a
first letter of each word is a good programming discussion of primitive data types with a few
practice. examples of data that they can handle.
Primitive Data Types
3. Your identifier must contain no spaces.
There are eight primitive data types supported
Java Keyword
by Java. Primitive data types are predefined by
Keywords in Java are reserved for specific uses. the language and named by a keyword.
The Java compiler recognizes these keywords
byte:
for their specific purpose so programmers are
not allowed to use them as identifiers. • Byte data type is an 8-bit signed two's
complement integer.
abstract boolean break
• Minimum value is -128 (-2^7)
byte case catch
• Maximum value is 127 (inclusive)(2^7 -1)
char class const
• Default value is 0
continue default do
• Byte data type is used to save space in large
double else extends
arrays, mainly in place of integers, since a byte
finals finally float is

for goto if • four times smaller than an int.

implements import instanceof • Example: byte a = 100 , byte b = -50

int interface long

naive new package


short: • This type is used when a wider range than int
is needed.
• short data type is a 16-bit signed two's
complement integer. • Default value is 0L.

• Minimum value is -32,768 (-2^15) • Example: long a = 100000L, int b = -200000L

• Maximum value is 32,767 (inclusive) (2^15 -1) float


• Short data type can also be used to save
memory as byte data type. A short is 2 times • Float data type is a single-precision 32-bit IEEE
754 floating point.
• smaller than an int
• Float is mainly used to save memory in large
• Default value is 0. arrays of floating point numbers.

• Example: short s = 10000, short r = -20000 • Default value is 0.0f.

Int • Float data type is never used for precise


values such as currency.
• int data type is a 32-bit signed two's
complement integer. • Example: float f1 = 234.5f

• Minimum value is - 2,147,483,648.(-2^31) double

• Maximum value is • double data type is a double-precision 64-bit


2,147,483,647(inclusive).(2^31 -1) IEEE 754 floating point.

• Int is generally used as the default data type • This data type is generally used as the default
for integral values unless there is a concern data type for decimal values, generally the
about
• default choice.
• memory.
• Double data type should never be used for
• The default value is 0. precise values such as currency.

• Example: int a = 100000, int b = -200000 • Default value is 0.0d.

Long • Example: double d1 = 123.4

• Long data type is a 64-bit signed two's boolean


complement integer.
• boolean data type represents one bit of
• Minimum value is - information.
9,223,372,036,854,775,808.(-2^63)
• There are only two possible values: true and
• Maximum value is 9,223,372,036,854,775,807 false.
(inclusive). (2^63 -1)
• This data type is used for simple flags that Variable
track true/false conditions.
• Variables are identifiers whose values can be
• Default value is false. changed. The general syntax for declaring a
variable is:
• Example: boolean one = true
datatype ( space ) identifier ;
char
• A variable provides us with named storage
• char data type is a single 16-bit Unicode that our programs can manipulate.
character.
• Each variable in Java has a specific type, which
• Minimum value is '\u0000' (or 0). determines the size and layout of the variable's
• Maximum value is '\uffff' (or 65,535 inclusive). memory; the range of values that can be stored
• Char data type is used to store any character. within that memory; and the set of operations
• Example: char letterA ='A' that can be applied to the variable.

Reference Data Types • The basic form of a variable declaration is


shown here:
• Data types not included among the primitive
data types are called reference data types and datatype variablename;
they consist of objects and arrays. String is a
datatype variablename = valuehere;
reference data type often used in many
applications. It is actually a class that lets us Kinds of Variables in Java
store a sequence of characters.
• Local variables
• Reference variables are created using defined
• Local variables are declared in methods,
constructors of the classes.
constructors, or blocks.
• They are used to access objects.
• Local variables are created when the method,
• These variables are declared to be of a constructor or block is entered and the variable
specific type that cannot be changed. will be destroyed once it exits the method,
constructor or block.
Example:
• Access modifiers cannot be used for local
• Class objects, and various type of array variables.
variables come under reference data type.
• Local variables are visible only within the
• Default value of any reference variable is null.
declared method, constructor or block.
• A reference variable can be used to refer to
any object of the declared type or any • Local variables are implemented at stack level
compatible type. internally.

• Example: Animal animal = new


Animal("giraffe");
• There is no default value for local variables so • Static variables are stored in static memory. It
local variables should be declared and an initial is rare to use static variables other than
value should be assigned before the first use. declared final and used as either public or
private constants.
Instance variables
• Static variables are created when the program
• Instance variables are declared in a class, but
starts and destroyed when the program stops.
outside a method, constructor or any block.
Java Literals
• When a space is allocated for an object in the
heap, a slot for each instance variable value is • A literal is a source code representation of a
created. fixed value. They are represented directly in the
code without any computation.
• Instance variables are created when an object
is created with the use of the keyword 'new' • Literals can be assigned to any primitive type
and destroyed when the object is destroyed. variable. For example:

• Instance variables hold values that must be • Byte a = 68;


referenced by more than one method,
constructor or block, or essential parts of an • Char a = ‘a’;
object's state that must be present throughout • String literals in Java are specified like they are
the class. in most other languages by enclosing a
• Instance variables can be declared in class sequence of characters between a pair of
level before or after use. double quotes. Examples of string literals are:

• Access modifiers can be given for instance • “hello world”, “Welcome \n to\n Java
variables. Programming “

Class/static variables • int a, b, c; // Declares three ints, a, b, and c.

• Class variables also known as static variables • int a = 10, b = 10; // Example of initialization
are declared with the static keyword in a class, • byte B = 22; // initializes a byte type variable
but outside a method, constructor or a block. B.
• There would only be one copy of each class • double pi = 3.14159; // declares and assigns a
variable per class, regardless of how many value of PI.
objects are created from it.
• char a = 'a'; // the char variable a iis initialized
• Static variables are rarely used other than with value 'a'
being declared as constants. Constants are
variables that are declared as public/private,
final and static. Constant variables never change
from their initial value.
Remember:

• 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 describes the
behaviors/states that object of its type support.
• A class can contain any of the following
variable types.

• Local variables: Variables defined inside


methods, constructors or blocks are called local
variables. The variable will be declared and
initialized within the method and the variable
will be destroyed when the method has
completed.

• Instance variables: Instance variables are


variables within a class but outside any method.
These variables are instantiated when the class
is loaded. Instance variables can be accessed
from inside any method, constructor or blocks
of that particular class.

• Class variables: Class variables are variables


declared with in a class, outside any method,
with the static keyword.

You might also like