Skip to main content

Naming Conventions and Indentation in Java

Quality coding Standards

Naming Rules

a) Declare name for a class in Java     class Book
     classs EnglishBook
     class Car
     class Student
     class StudentTranscript


b) Variables/function Name
     int book
     int englisBook
     int enonomyOfAmerica
     int car
     int student
     int studentTranscript


c) Constants (AllUpper)
    final int A=5;
    final int SHOAIB=5;
    final int ECONOMYOFPAKISTAN=34;

.....................................
Indentation in Java Programming
  class Test{
  }
void f (){
  }
  while (a>b){
 
  }
 .....................................
 Data Types in Java
  1- int (4 bytes)  short (2 byte)  long (8)
  2- float (4bytes) 2.5f
      float a=2.5f;
  3- double (8 bytes) 2.5
  4- byte ( 1 byte)
  5- char (2 bytes) unicode ( Range 0 to 65536)
     'a' '\n'
     '\uxxxx'   '\u2345'  
    '\u' Followed by exactly four hexadecimal digits
     
  6- boolean  (true, false)

.....................................
Type Conversion in Java    Two Methods
     a) Coercion
     b) Casting

    
1- Coercion
     
Implicit type conversion by compiler
      int a=5;
      float b=a;//promotion
      double g=2.5;
      int y=g;//demotion (data loss)
               // not allowed
     float a=2.5; //demotion
                  //not allowed
 
2- Casting
      Explicit type conversion by programmer

       double b=2.5;
       int x=(int)b;

....................................
Practice 1
class Test{
public static void main(String o[]) {
float a=2.5;

}
}
...................................
Practice 2
class Test{
public static void main(String o[]) {
float a=2.5f;

}
}
.....................................
Practice 3
class Test{
public static void main(String o[]) {
float a=2.5f;
int b=a;

}
}
.........................
Practice 4
class Test{
public static void main(String o[]) {
float a=2.5f;
int b=(int)a;
}
}
....................................
Some Hint from C++ 
void main() {
int i=1;
int sum=0;
while(i<=3){
float j;
cin>>j;
sum=sum+j;
i++;
}
cout<<sum;
}

Comments

Popular posts from this blog

Generalization in java with examples

      Generalization basic points  Type/Subtype Relationship  "IS A " Relationship  One class has many subclasses   General class can have many Special classes as  subclasses  General names can be used to Address special names  Top to bottom hierarchy goes towards specialization  Bottom to top Hierarchy goes towards generalization e.g         Soap     Bath Soap and Washing Soap is a subclass of Soap e.g COldDrink   Pepsi      Team -Cold Drink is a general class -Pepsi and team are special classes e.g    Person Teacher       Student e.g            Car     Sedan           Hatchback Honda   Suzuki     Suzuki   Toyota civic        Liana      Alto      vitz ..........................................

Aggregation in java with example codes

What is aggregation?   "Have a" or "May have" relationship  Aggregation is special type of association  Life time of Component object is independent from its  container object E.g A school has one student class  Student  { String name; Student  () { name= null ;  } Student  ( String n ) { name=n;  } void  setName ( String n ) { name=n; } String getName () {  return  name; } } class  School { Student  st; School  (  ) {  st= null ; } School  ( Student s ){ st=s; } void  studentName (){    System.out.println ( st.getName ()) ; } } class  Post { public static  void  main  ( String a []){ Student st= new  Student ( "Imran" ) ; School s=  new  School ( st ) ; s.studentName () ; } } Output Hello ...

Life Time of Objects in Java with code Examples

Lifetime Concept in Java Programming Time between allocation and de-allocation of memory Life time depends on block i.e { }  life time starts when  declaration statement elaborated and expires at end of block   There is no delete keyword in java  Garbage collector is responsible for all deletes Java is free from dangling reference and garbage problem Why java does not support delete keyword? Java does not support delete keyword because there is a garbage collector in java This automatically runs and collects dangling or un-referenced objects.That is why there is no delete keyword in java like C++. Garbage collector can be explicitly like that System.gc();  When a life of object ends garbage collector runs automatically Example 1 { int a=5;// life Time start here a++; } //life time expire Example 2   Let Student is a class Student s;   // creating a reference of Student class    {Student t; //...