Basic of Java - 1
Rakesh Jha (Product Head/Chief Architect)
Product Head | Driving Industry Innovation | Architecting Multi-Geography B2B2C Ecommerce Ecosystems | Expert in Business Strategy, Market Analysis, Technical Leadership and Revenue Generation
Baisc of Java
1. Download jdk
2. install jdk
3. set ENVIRONMENT_VARIABLE (JAVA_HOME) - PATH OF JAVA installation (till bin)
4. create a simple "hello world" program
5. compile - javac filename.java
6. run - java filename
Basic syntax -
public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}
ENUM Example -
class LatestFreshJuice {
enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
FreshJuiceSize size;
}
public class FreshJuiceTest {
public static void main(String args[]) {
LatestFreshJuice juice = new LatestFreshJuice ();
juice.size = LatestFreshJuice .FreshJuiceSize.MEDIUM ;
System.out.println("Size: " + juice.size);
}
}
Comment in Java
================
public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output
* This is an example of multi-line comments.
*/
public static void main(String []args) {
// This is an example of single line comment
/* This is also an example of single line comment. */
System.out.println("Hello World");
}
}
public class MainActivity
{
public static void main(String []args)
{
System.out.println("Hello World");
// prints Hello World - Single line comment
/*
* Hi, I'm multiline commnet
* you can use multiline comment in this way
*/
}
}
Keyword in Java
================
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
Mostly used Keyword in Java
================================
abstract boolean break
byte case catch char
class const continue default
do double else
extends final finally float
for if implements
import instanceof (rare) int interface
long new package
private protected public return
short static super
switch synchronized (rare) this throw
throws try void
while
OOPS concept
============
Java is an Object-Oriented Language. As a language that has the Object-Oriented feature, Java supports the following fundamental concepts -
Polymorphism
Inheritance
Encapsulation
Abstraction
What is class
============
A class is a blueprint, and virtual world Entity from which individual objects are created.
Following is a sample of a class.
public class Dog {
String breed;
int age;
String color;
void barking() {
}
void hungry() {
}
void sleeping() {
}
}
Create Object
=================
public class Puppy {
public Puppy(String name) {
//special method called Constructor
//foo(), abc(), barking(), sleep() and constructor is used for assignment
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public static void main(String []args) {
// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" ); //parameterized constructor
//Puppy myPuppy = new Puppy(); // Default constructor
}
}
public class Puppy {
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public Puppy(String name, int age) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
System.out.println("Passed Age is :" + age );
}
public static void main(String []args) {
// Following statement would create an object myPuppy
Puppy myTommy = new Puppy( "tommy" );
Puppy myRommy = new Puppy( "Rommy", 5);
}
}
/* First create an object */
ObjectReference = new Constructor();
/* Now call a variable as follows */
ObjectReference.variableName;
/* Now you can call a class method as follows */
ObjectReference.MethodName();
Access Instance variable & Method
=========================
public class Puppy {
int puppyAge;
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Name chosen is :" + name );
}
public void setAge( int age ) {
puppyAge = age;
}
public int getAge( ) {
System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}
public static void main(String []args) {
/* Object creation */
Puppy myPuppy = new Puppy( "tommy" );
/* Call class method to set puppy's age */
myPuppy.setAge( 2 );
/* Call another class method to get puppy's age */
myPuppy.getAge( );
/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}
public class PuppyCallVariableAndMethod {
int puppyAge;
public PuppyCallVariableAndMethod(String name) {
// This constructor has one parameter, name.
System.out.println("Name chosen is :" + name );
}
public void setAge( int age ) {
puppyAge = age;
}
public int getAge( ) {
System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}
public static void main(String []args) {
/* Object creation */
PuppyCallVariableAndMethod myPuppy = new PuppyCallVariableAndMethod( "tommy" );
/* Call class method to set puppy's age */
myPuppy.setAge( 2 );
/* Call another class method to get puppy's age */
myPuppy.getAge( );
/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}
Practice
=========
import java.io.*;
public class Employee {
String name;
int age;
String designation;
double salary;
// This is the constructor of the class Employee
public Employee(String name) {
this.name = name;
}
// Assign the age of the Employee to the variable age.
public void empAge(int empAge) {
age = empAge;
}
/* Assign the designation to the variable designation.*/
public void empDesignation(String empDesig) {
designation = empDesig;
}
/* Assign the salary to the variable salary.*/
public void empSalary(double empSalary) {
salary = empSalary;
}
/* Print the Employee details */
public void printEmployee() {
System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" + designation );
System.out.println("Salary:" + salary);
}
}
import java.io.*;
public class EmployeeTest {
public static void main(String args[]) {
/* Create two objects using constructor */
Employee empOne = new Employee("James Smith");
Employee empTwo = new Employee("Mary Anne");
// Invoking methods for each object created
empOne.empAge(26);
empOne.empDesignation("Senior Software Engineer");
empOne.empSalary(1000);
empOne.printEmployee();
empTwo.empAge(21);
empTwo.empDesignation("Software Engineer");
empTwo.empSalary(500);
empTwo.printEmployee();
}
}
There are two data types available in Java -
Primitive Data Types
Reference/Object Data Types
Primitive Data type -
byte (8 bit)
short (16 bit)
int (32)
long(64)
float(single precision 32 bit)
double(double precision 64 bit)
boolean (1 bit)
char (single 16 bit unicode)
Reference Data type
==================
Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy, etc.
Class objects and various type of array variables come under reference datatype.
Default value of any reference variable is null.
A reference variable can be used to refer any object of the declared type or any compatible type.
Example: Animal animal = new Animal("giraffe");
Variable type
==============
int a, b, c; // Declares three ints, a, b, and c.
int a = 10, b = 10; // Example of initialization
byte B = 22; // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a'; // the char variable a iis initialized with value 'a'
Local variables
Instance variables
Class/Static variables
Next session will be tomorrow - Basic of Java - 2