Variables and Datatypes
11:01 AM
Posted by Sai Subramanyam
Variable and its Scope
There are mainly 2 types of variables in Java.They are: Instance variables and Member Variables(Class variables).
Instance Variables are the variables which are created while instantiating a class.
Class variables are the normal variables which are declared casually inside a class.
Note : The static variables also come under class variables.
Datatypes Explained
In the previous section we came across the various datatypes and its categories.Lets have a deep look at them.
- Integer Datatypes(int,byte,short,long) are used to store signed integers values.The memory size of the datatypes of byte,short,int,long are 1,2,4,8 respectively.
- Floating Point Datatypes(float,double) are used to store the decimal type(fractional type) values.The memory size of the datatypes of float,double are 4,8 bytes respectively.The floating point literals float must end with ' f '.And double literal must take a precission of two places or else there will be a Loss In Precesion Error by compiler.
- Character Datatype(char) are used to store single Unicode characters.The memory size is 2 bytes.
- Boolean Datatypes are used to store the values which are either true or false .
Java Language Basics
10:18 AM
Posted by Sai Subramanyam
In the previous posts we saw the basics of Object Oriented Programming , and Demo Java Programs.Now its the time to dive up into the Java Language Basics.Java language basic means the whole theory wound up around the object,so lets start with the objects and variables.
Object Instantiation
An object as quoted in my early posts is a real world run time entity.It can also be defined as an instance of a class.
In easy words instantiation is not but creation or representation.So,we will see just how to create an object.By creating an object you can access the members and methods of a class.
We generally create an object for a class say Sample,then the object is represented as :
Syntax : ClassName <reference variable> = new Constructor;
Example : Sample s = new Sample( );
Here Sample is the class-name , s is the reference variable and Sample() is the constructor
Constructor is defined as the block of code which is initialised whenever an object is created.
So,what actually happens when you create an object.If you observe carefuly we are using a dynamic memory allocation operator known as new here which allocates the memory for the created object.Here we will store all the member variables and the method values.
If we create multiple instances for a class we give different reference variables while instantiating.
Example : Sample s1 = new Sample();
Accessing the members of the class by using an object
Now, we created the object.We should know what to do with the object and how to access the members by using the object.
We can access members of a class by using an object by using the DoT operator( . ).
Syntax : <reference-variable>.classVariable;
<reference-variable>.classMethod();
Example : s.width;
s1.width;
Example : s.myMethod();
s1.myMethod();
Tokens
Tokens are the smallest elements in the Java program.These tokens are determined into several types.We will take a brief look over the list.So, they are :
- Identifiers : Any names which are provided by the user.In Java identifier can be of infinite-length. In th declaration int a , b, c; (a,b,c) are identifiers.
- Keywords : Java provides 60 keywords,which are used while programming.We will come to know as we dig deep into the Java Language.
- Literals : These are the elements used frequently in the program.They can be divided as Numeric Literals and Character Literals(Boolean Literals).We will see this Literals once we cover the last token Datatypes.
- Operators : These are the elements which are specially used for the evaluations of the variables.Java supports wide range of Operators.
- Seperators : These are used to end ,interept or join two statements in Java.In simple words,it will inform the Java compiler how to group the statements and the code(If you want more detail explanation,better go and refer Compiler Design( :P ) ).
- White Spaces : Not very tough to understand isn't it?
- Data Types : These are the stroage structures of the variables.Every variable is associated with a Datatype in Java.
- Primitive or Non Reference Datatypes :byte,short,char,boolean,float,int,double,long( ordered in ascending and alphabetical order)
- Reference Datatypes : Class type, Interface type,Array type,String Type.
Hello World App
1:48 AM
Posted by Sai Subramanyam
Hello again,here now we will deal with how to create a simple Java Hello World App which displays the message "Hello World!".To create this program, you must :
- Create a Source file : The source file contains all the code of our program written in Java.You can generally use a text editor like Notepad for creating the source file.After writing the code just save it in the .java format.Example: Sample.java
- Compilation of the Source file : Now the created source file(Sample.java) must be compiled to generate the ByteCode.It will be compiled by javac compiler which can only be understood by Java Virtual Machine(J.V.M).The command for compilation is javac Sample.java
- Run the java file : The java application can be executed by J.V.M by using java command.The command for running the file is java Sample.
/**
* The HelloWorldApp class implements an
* application that simply prints
* "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
// Display the string.
}
}
Then, save the file as HelloWorldApp.java and exit NotePad.
Compilation of the Source File
Bring up the command prompt and navigate into the source file location.
Then,type the command- javac HelloWorldApp.java
- java HelloWorldApp
- The HelloWorldApp source file contains Multiple line Comments which are useful in the development phase of the program.Its a good habit to Comment your programs as it is useful for others to know about your program.
- HelloWorldApp class begins with the keyword class(A brief explanation will follow the next posts) which is embeded between the two curly braces " { } ".
- Main Method is the essential method for every class.It is the entry point into the program.It is named as "public static void main(String [ ] args)".The main reason behind this is:
This is the introductory Hello World App.Stay Tuned for more Tutorials!
Au Revoir!!Introduction to Java
12:54 AM
Posted by Sai Subramanyam
Internet, is the world wide network of computers.It mainly connects millions of computers all over the world.So these millions of users have many different types of computers , operating systems with different architecture , accessability.So,there is a huge need for a programming language which is suitable for all these requirements.
- At this moment ,Sun Microsystems,Inc. designed a programming language called Oak by James Gosling and Patrick Naughton which is renamed to Java in 1995.
- How Java Works?
- Simple.
- Object Oriented.
- Distributed.
- Interpreted.
- Robust.
- Secure.
- Architecture Neutral.(Platform Independency)
- Portable.
- High Performance.
- MultiThreaded.
- Dynamic.
- Java Source File is categorised into four sections.They are:Package section,Import section,Class/Interface section,Comments which are further divide into Multiple Line Comments and End of Line Comments.You can refer the above picture to make it clear.




