Showing posts with label Java Basics. Show all posts
Showing posts with label Java Basics. Show all posts

Variables and Datatypes

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.
          Note : A long literal is generally ended with ' L '.Ex: 32L.
  • 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.
          Example : 2.8f - float literal.
                         2.85 - double literal.
  • 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 .

Video tutorial:

So,we covered the Language basics till here and from next parts we will go into the more serious parts which are useful in coding.

Stay Tuned for more posts!!

Au Revoir!!




Java Language Basics

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 : 



  1. 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.
  2. Keywords : Java provides 60 keywords,which are used while programming.We will come to know as we dig deep into the Java Language.
  3. 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.
  4. Operators : These are the elements which are specially used for the evaluations of the variables.Java supports wide range of Operators.
  5. 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 ) ).
  6. White Spaces : Not very tough to understand isn't it?
  7. Data Types : These are the stroage structures of the variables.Every variable is associated with a Datatype in Java.
             There are mainly 2 types of Data types.They are :
  • 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.
                  We will deal with this in depth and detail when we are discussing the variables.i.e;in the next post.Stay tuned till then! Au Revoir!!

Hello World App

Creating you First Java Application.


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.

Creating a source file

To create a source file , just open the notepad and type the following code:

HelloWorldApp.java :
/**
 * 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
Run the Java File
To run the Java File,after compilation you can see the output by typing the command.
  • java HelloWorldApp
Explaining 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 " { } ".
class HelloWorldApp{ }
  • 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:
-> Why main must be declared as public?
A). main() is invoked by JVM(i.e. J.V.M will access the main() method in our class)As JVM is outsider,it does not belong to our program package.So,inorder to access main outside our program we declare it as public.Or else we will get a compilation error known as main method not public.
-> Why main must be declared as static?
A).If a method is declared as static in the class we can call it with using classname.methodName(); So in order to call the main method it must be static.
-> Why main must be declared as void?
A). A void keyword means it returns nothing.JVM expects nothing when calling the main method.So it is named as void.
This sums up the introduction to the basic Java Program.
This is the introductory Hello World App.Stay Tuned for more Tutorials!

Au Revoir!!

Introduction to Java



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.
Java mainly ignores the barriers between different computer chips and operating systems.Your applications wil become compatible with all kinds of operating systems and types of computers.

Java is used everywhere.Right from your mobile phone to the computers.
  • How Java Works?
        The Java code is generally compiled by using a javacompiler(JIT).The compiled code is known as ByteCode.This ByteCode can be executed in any platform irrespective of the architecture,compatibility,power and capacity.


This ByteCode obtained from the compiler is executed in the Java Virtual Machine(J.V.M) which is the Java run time system which also acts as an interpreter for the ByteCode     .      

Characteristics of Java       
  • Simple.
  • Object Oriented.
  • Distributed.
  • Interpreted.
  • Robust.
  • Secure.
  • Architecture Neutral.(Platform Independency)
  • Portable.
  • High Performance.
  • MultiThreaded.
  • Dynamic.
Closer Look at Java Source File

     A java code is written mainly in the text editors or an IDE(Integrated Developer Environment).If you are at learning stage I suggest you to start with a text editor like Notepad,EditPlus,etc;


  • 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.
Here is a small Video on Basic Java Development to wind up this post.

Hello World Program!