Classes and Objects
A class is a template or blueprint. For example, a class called Student will have objects like name, roll number, marks etc.
<class modifiers> class <class name> [extends clause] [implements clause]
The UML (Unified Modeling Language) notation for the class is:
<class name> <fields> <Methods>
//Constructor Box(int, int, int)
Box(int w, int h, int d)
width = w;
height = h;
An instance of a class is called an object. In Java, the object is created using ‘new’ keyword. The object creation process involves following three steps:
- Declaring reference variable
- Construction an object using ‘new’ keyword.
- Assigning Reference value (address) to reference variable.
Box b = new Box(1,2,3);
- The fields of a class are also called an attribute, properties, instance variable, or data member.
- The methods of a class are also called as behavior, operation, instance method, or method member.
- The data member and method member of a class are called a member of a class.
- The new keyword creates an object and returns the reference value to the reference variable.
The name of a program is called an Identifier. An identifier is a sequence of characters. Identifiers are the names of the variables, methods, classes, packages, interfaces, arrays, enumerations, labeled names, etc.
public static void main(String args)
int age = 10;
In java, the data types are grouped into two categories:
- Primitive types
- Reference types =>Classes, Interfaces, Exceptions, Errors, Enums, Annotations, and Arrays
There are 9 primitive types in java.
Below table summarizes the categories of the primitive types:
integers byte, short, int, long floating point numbers float, double Characters char Booleans boolean empty set data type void
1. By default, all integers are initialized with 0.
2. By default, all floating point numbers are initialized with 0.0
3. By default, all characters are initialized with blank space.
4. By default, all booleans are initialized with false.
5. By default, all reference types are initialized with null.
6. The size of the primitive data types is fixed across all operating systems.
7. The java supports only signed integers but unsigned characters.
The keyword has a predefined meaning in the programming language. In jdk1.6, there are 50 keywords and 3 reserved literals.
The below table summaries keyword category for our better understanding:
Primitive data type
|byte, short, int, long, float, double, char, boolean, void
if, else, switch, case, default, while, do, for, break, continue, return
try, catch, finally, throw, throws, assert
private, protected, public, final, static, abstract, interface, strictfp, native, transient, volatile, synchronized
class, extends, implements, package, import, enum
new, this, super, instanceof