Posts

Variables

Variable is name of reserved area allocated in memory. Example:      int var =20; //Here var is a variable There are three types of variables in java: local variable instance variable static variable Local Variable A variable that is declared inside the method is called local variable. Instance Variable A variable that is declared inside the class but outside the method is called instance variable. It is not declared as static. Static variable A variable that is declared as static is called static variable. It cannot be local. Example to understand the types of variables class A {  //instance variable  int data=50;  //static variable  static int m=100;  void method()  {    //local variable    int n=90;  } //end of class }

Reference Data Types

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 data type. Default value of any reference variable is null. A reference variable can be used to refer to any object of the declared type or any compatible type. Example: Animal animal = new Animal("giraffe");

Data Types

The term data type refers to the type of data that can be stored in a variable. Sometimes, Java is called a “strongly typed language” because when you declare a variable, you must specify the variable’s type. Then the compiler ensures that you don’t try to assign data of the wrong type to the variable. There are two data types available in Java: Primitive Data Types Reference/Object Data Types Primitive Data Types There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword. 1. byte: Byte data type is an 8-bit signed two's complement integer. Minimum value is -128 (-2^7) Maximum value is 127 (inclusive)(2^7 -1) Default value is 0 Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int. Example: byte a = 100 , byte b = -50 2. short: Short data type is a 16-bit signed two's complement integer. Minimum value is

Naming Conventions

Image
A naming convention is a rule to follow as you decide what to name your identifiers (e.g. class, package, variable, method, etc..). Different Java programmers can have different styles and approaches to the way they program. By using standard Java naming conventions they make their code easier to read for themselves and for other programmers. Cases in Java Naming Convention: Using the right letter case is the key to following a naming convention: Lowercase is where all the letters in a word are written without any capitalization (e.g., while, if, mypackage). Uppercase is where all the letters in a word are written in capitals. When there are more than two words in the name use underscores to separate them (e.g., MAX_HOURS, FIRST_DAY_OF_WEEK). CamelCase (also known as Upper CamelCase) is where each new word begins with a capital letter (e.g., CamelCase, CustomerAccount, PlayingCard). Mixed case (also known as Lower CamelCase) is the same as CamelCase except the fir

Java Keywords

Reserved Keywords in Java Keywords are special tokens in the language which have reserved use in the language. Keywords may not be used as identifiers in Java.You cannot declare a field whose name is a keyword, for instance. Below are all the Java language keywords: abstract assert boolean break byte case catch char class const continue default do double else enum extends f inal 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 goto and const - are not used enum - is present s

Comments in Java

Comments :         /*        * The HelloWorldApp class implements an application that        * simply displays "Hello World!" to the standard output.        */        class HelloWorldApp        {        public static void main(String[] args)        {        System.out.println("Hello World!"); //Display the string.        }        }     The Java language supports three kinds of comments: /* text */ The compiler ignores everything from /* to */. /** documentation **/ This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation. // text The compiler ignores everything from // to the end of the line.

Hello World!!

Hello World     class HelloWorld    {     public static void main(String args[])     {     System.out.println("Hello, World!");     }    } If all goes well, you should see the following output: Hello, World! Explaination: All code is contained within a class, in this case HelloWorld. The file name must match the class name and have a .java extension, for example: HelloWorld.java. All executable statements are contained within a method, in this case named main(). Use System.out.println() to print text to the terminal. Note: Classes and methods (including other flow-control structures) are always defined in blocks of code enclosed by curly braces { }. All other statements are terminated with a semi-colon (;). Java language is case-sensitive! This means that HelloWorld is not the same as helloworld, nor is String the same as string.