Pages

Friday 2 August 2019

Java basics

Agenda:
  1. Class structure
  2. Identifiers
  3. Modifiers
  4. Members
  5. Constructor
  6. Package
  7. Import statement
  8. Comments
Class Structure:
Class: It is a design(blueprint) from which we can create an object.
Purpose of the class In java, using keyword class we can map real-world entities.

Define a class in java:
[Access Modifiers][Non Access Modifiers]<class><identifiers>{}[;]
class Student{}
Note: [] represents optional, <> represents mandatory

Identifiers:
Any name in java is an identifier. Name could be package name,class name,method name,variable name,argument name. 
Note: Name should be any logical name, which means developers can give any name.

Recommended:
  • The class name should be Noun and starts with Capital letter.
    e.g Person, Student
  • VariableArgument name should be Noun and start with a small letter. 
  • If the name is a combination of two or more words then follow the camel case.
    e.g: totalAmount, totalMarks
  • The method name should be a verb and start with a small letter.
  • If the name is a combination of two or more than two words, follow the camel case.
    e.g: getName()
Rules for identifier:
Rule 1:  Allow characters for identifiers.
  a.Capital Alphabets A-Z.
  b.Small Alphabets a-z.
  c.Numbers 0-9
  d.Allowed special characters $, underscore(_)
Rule 2: Identifiers should start with Alphabet (small or capital) or any currency sign like $ and underscore(_).
Rule 3: No restriction on the length of identifiers.
Rule 4: Reserve keywords are not allowed.
Rule 5: No restriction if you are using inbuilt classes or methods names.
Rule 6: Use underscore(_) at any positions(start,mid,end).
Rule 7: Use currency sign at any positions(start,mid,end)

Modifiers:
Access Modifiers:
  1. private 
  2. protected
  3. public
Non Access Modifiers:
  1. transient
  2. volatile
  3. final
  4. abstract
  5. native
  6. synchronized
  7. strictfp
  8. static
Q1: Valid modifiers for class.
Ans: public,abstract final,strictfp (FAPS)

Q2: Valid modifiers for method
Ans: public, protected, private, abstract, static, final, synchronized, native & strictfp (PS3FAN)

Q3:Invalid modifiers for method
Ans: transient, volatile (TV)

Q4:Valid modifiers for variable
Ans: public, protected, private, static, final, transient & volatile

Q5: Invalid modifiers for variable
Ans: native,abstract,synchronized,strictfp(NASS)

Reserved keywords
1. Data types related keywords(8)
byte,short,int,long,float,double,char, boolean

2.Modifiers related keywords(11)
private,protected,public,transient,volatile,final,abstract,native,static,strictfp,synchronized

3.Flow control related keywords(11)
if,else,switch,case,default,for,do,while,return,break,continue

4.Exception handling related keywords(6)
try,catch,finally,throw,throws,assert

5.Object related keywords(4)
new,instanceof,this,super

6. Class related keywords(6)
package,class,enum,interface,extends,implements

7.Reserve keywords for literals
true,false,null(3)

8.Unused keywords
go,const(2)

Object:
  • It is an instance of the class.
  • It has properties(variables) and behaviors(methods)
Variable:
It is used to store the value(state) of an object.

Types of variables:
1. Instance variable/non-static variable: If a static keyword is not used with a variable then it is called non-static variable
2. Static variable: If the static keyword is used with a variable then it becomes static variable

Declaration of a variable:
[Modifiers] <Data type> <variable name><;>
e.g int age;

Define a variable:
[Modifiers] <Data type> <variable name> <=> <literal(const value)><;>
int age=1;

Method: If you want to perform an action then define the method and inside the method write your business logic.

Declaration of a method:
[Modifiers]<return type> <method name><([arguments])><;>
void set(int a);

Define a method:
[Modifiers]<return type><method name><([arguments])><{}>[;]
Note: If return type is other than void then you must use return keyword to return the value.
e.g int get(){
return 10;
}

Blocks:
Types of blocks
a) non-static/instance block
Purpose of instance block: To initialize the variables.
e.g {}
b) static block
Purpose of static block: To initialize the static variables and if you want to verify class is loaded or not, use static block.
e.g static {}

Constructor:
It is used to initialize the variables.

Declaration of a constructor:
[Access Modifier] <class name><([arguments])><;>
Student();

Define a constructor:
[Access Modifier]<class name><[arguments])><{}>[;]
e.g: Student(){}

Package:
It is an abstraction mechanism to group related classes and interfaces into a single module
Purpose of the package:
  • To resolve name conflicts
  • To improve the modularity of the application
Important points to remember about package statement:
1. Only one package statement is allowed in java program otherwise you will get a compiler error
2. In any java program, the 1st non-comment statement should be the package statement. (If it is available)

Declaration of package statement:
<package><package name><;>
e.g:package book;

Import statement:
Purpose: To improve the readability of the program.
You can import a class in two ways. For example import  ArrayList.
a) import java.util.ArrayList;(recommended)
b) import java.util.*;(not recommended)

Points to Remember:
1. In any java program following two packages are not required to import. By default, these are available to every java program.
 a.java.lang;(package)
 b.default package(current working directory)
2. Whenever we are importing a package all classes and interfaces present in that package are by default available, but not sub package classes or interfaces.

static import statement:
1. It was introduced in java 1.5v.
2. If you want to access static members of the classes then you can use static import.
Declaration:
<import><static><fully qualify class name><.><static members name><;>
e.g: import static java.lang.System.out;
Note:Here order is important.

Comments:
1.Single line comment: //
2.Multi line comments: /* */

Note: //System.out.println(\u000d); Compiler Error.(this character(CR) is pre processed.Even before the compiler transforms the source
code)

Order of java class components(package statement,import statements,comments,class definition, variables,constructors,methods).
  1. package statement
  2. import statement
  3. class definition
  4. variables,constructors,methods(FCFS)
    Note: No restriction on comments, It could be at any position.

Q: How can you access the members of the class?
Ans: Using object reference.

Q: How can you create an object of a class?
Ans:Using new operator.

Write syntax for object creation.
<class name> <identifier> <=> <new><class name>();
e.g: Student student = new Student();

Q: How can you access/call/invoke a method.
Ans: <object reference><.><method name>();
e.g:  student.m1();

Q: How can you call static members of the class?
Ans: <class name><.><member name>
Note: you can also call by object reference but it is not recommended.


Q: How can you test your class functionalities?
Ans: using main() method.

Q: What is the syntax of main() method?
Ans: public static void main(String[] args){}

Q: How can you identify the java source files?
Ans: By seeing extension (.java)

Q: In a java source file, how many classes you can define?
Ans: No restriction, As much as you can define.

Q: In a java source file, how many public classes you can define?
Ans: Only one.

Q: If you define a class using public modifier then what should be your java source file name?
Ans: Class name.
Q: If in a java source file many non-public classes are defined then what should be your file name?
Ans: No restriction, Any name.

Q: How can you compile your java program?
Ans: javac  <file name><.java>

Q: How can you run your java program?
Ans: java  <file name>

Next Topic: DATA TYPES