Session 3: Java Basic Concepts


Java Basic Concepts

Q? what are the syntax for Java Language
/> /*long comments are done in such a way */
/> // short comments on one line
/> { code block and defines one scope}
/> control statements used are same as in C programming language :
if-else, switch, while, do-while, for, break, continue
/> boolean are used to represent logic values (true,false)
we cannot evalute this condition: int x = 2; if (x) {…...........}
instead, we have to use relational operators : if(x!=0) {….......do something.......}
/> parameters are always passed by value and they can be primitive type of object refrences
Note* : while passing parameter we just copy the object reference not the whole object
/> for constant we have to use final modifier: ex: final float AMOUNT = 3,000;
/> Operators used are also same as C :
arithmatical + - * / %
relational == != > < >= <=
bitwise % | << >> ~
assignment = += -= *= /= %= &= |= ^=
increment ++ decrement

/> logical operators : && || ! ^ (logical operators work only on boolean )


Q? what are the primitive types
/> the type which are defined in the language: ex: int, double, boolean,char, long, float, void, double (each primitive type has the specific dimension )
/> instance declaration : we first declare instance name, then the type and then allocate memory space for the reference ;

Q? how classes are defined and how objects are used
/> class is often called from the object descriptor and it consists of attributes and methods
/> object can pass and accept messages through methods and they may also have parameters
/> Classes are defined by developer (any) or can use the already existing class from the libraries
Car c ;
this allocates the memory space for the reference and sometimes it is initialized with null by default
/> now while using the object : the allocation and initialization of the object value are made by its constructor :
c = new Car();

Q? what is overloading and when does it occur.
/> having different methods with same name lead to overloading in java programming

Q? how can we avoid overloading
/> by having different signature : a signaure is made of Method name + Ordered list of parameter types .
So the idea is we can also have the method with same name But with different parameter types.

Q? what are the important properties of object
/> The Class identifies its object and also defines its structure
/> object is also identified by its internal unique identifier
/> zero, one or more reference can point to the same object but a refrence is Not an object
Q? how do we create a new object
/> we create object by a keyword new
Aeroplane a = new Aeroplane();
what this new keyword returns is a reference to the piece of memory containing the created object
this keyword also calls the constructor method of the object and this is with/without parameters

Q? what is used to store the data dynamically created at run-time
/> we use Heap memory for executing java programs
/> Instances(objects) created by keyword new are also in the heap memory

Q? what are constructor method
/> it contains operations (initialization of attributes ..) we want to execute on each object as soon as it is created.
/> attributes are initialized with default values here: before using it in any constructor
Numerics : 0 zero
Boolean : false
Reference : null
/> if a consructor do not have a declared a default constructor with no parameter is defined by default
/> constructor do not have declared a return type
/> overloading of constructors may occur so to avoid that we use different signature declaration

Q? how do we refer to the current object
/> we use the keyword this (this will refere to the object upon which the method has been invoked)
it do not work for the object that has not been invoked in the object

Q? how do we invoke the method on an object
/> by using dotted notation
ex: objectReference.Method(paramters)
/> Note* : if we are invoking the method with another method but under the same object then dotted notation is not required
/> to access the attributes also we use dotted notations : objectReference.attribute
/> and simillarly the methods accessing attributes of same object do not need to use object reference

Q? how can we use the combined dotted notations :
/> ex: System.out.println(“ Namaste, Niranjan ! ”);
System is a Class in package java.lang
out is an object of type PrintStream
println() is a method of PrintStream which prints a text line followed by a new-line

Q? what kind of operations are performed on refrences
/> we can use only relational operators == and !=
/> and this equality condition is evaluated on the values of references and not on the values of the objects

Q? String class in java.lang library
/> String is a array of characters and there is no primitive type to represent string
/> class String is not modifiable
/> class StringBuffer is modifiable
String s = new String (“ niranjan ”);
StringBuffer sb = new StringBuffer(“khatr”);
/> check out the java documentation for string for the information of String class methods and StringBuffer class methods




Q? how do we conactinate two strings
/> by using + operator :
/> this also helps to automatically convert to string : ex: “pi” + 3.14



Q? how modularization works in coding in java
/> ex: if we want to do Washing Machine coding design then: we have to design the concepts: we sperate them in modules: using modularity principle (cut- down inter-component interaction)
/> then we identify and deligate resoponsibilities to each components :
components = Classes , interation = read/write attributes , interaciton = calling a method

Q? how scoping work for members of class
/> private : member is visible and accessible from instances of the same class only
/> public : members is visible and accessible from everywhere

Q? how do we read/write a private attributes
/> we use setter method : to modify private attributes
/> we use getter method : to access private attributes

Q? package features
/> package is a logic set of class definitions
/> each package has its own new scope thus we can have same class names in different package without conflict

Q? how to give a package name
/> the convention way is to name in Internet name in reverse order (com.niranjan.www.myPackage)

Q? how to use package
/> we use package declaration at beginning of each class file :
/> where needed . We just use the import keyword at beginning of class file to use the required package
/> import packageName.className; // import single class
/> import java.awt.*; // import all classes but not sub packages

Q? what is default package
/> when no package is specified then the class belong to the default package
/> default package has no name
/> classes in default package cannot be accessed by classes residing in other packages
/> using default package is strongly discouraged

Q? what are the visibility or scope of packages
/> public class A{ }: class and public members of A are visible from outside the package
/> ____ class B {}: this is package visibility and class and any members of B are not visible from outside the package
/> private class A{ } the class and its members whould be visible to itself only : SO strongly discouraged to do this.

Q? what is array in java
/> in java array is an object and it is stored in the heap (so dimension are defined in run-time dynamically)
/> arrays are usually an ordered sequence of variables of the same type which are accessed through an index
/> array can contain both primitive types or object references

Q? how to declare array
/> int[] arrayname;
/> array declaration allocates the memory space for a reference and whose default value is null

Q? how to create array
/> we use the new operator to create new array
String[] s = new String[5];
/> statistically also we can initialize with values :
Ball[] b = {new Ball(“red”), new Ball(“green”)};

Q? operations performed in array
/> simple loop operations and many other operations can be performed
/>
int[] a= new int [5];
for (int i=0; i< a.length; i++ ){
a[i]= i;
}

Q? what are wrapper class types
/> this are the object versions of primitive types and define converions operations between different types
/> Boolean , Character , Byte, Short , Integer, Long, Float, Double, Void

Q? what are static attributes and methods
/> The comman properties which are common to all instances of an object.
This are defined with static modifier
static int countNum = 0;
/> static methods are not related to any instance and are mostly used to implement functions
/> to access static attributes and methods we use the name of the class; Moto.count Num ; Utility.inverse(20);

Note* : Enum can be declared outside or inside a class but NOT within a method
Enums are not String or ints but more like a kind of class but constructor can't be invoked direclty here.
public enum Suits {
SPADES, HEARTS, DIAMONDS, CLUBS
}

Comments

Popular Posts