Day 01:- Introduction To Java

DAY 01- Introduction to Java

========================================================================
TOPICS

  • Basic Java Program
  • Datatypes In Java
  • Data Members In Class
  • Member Functions In Class
  • Static and Instance DM and MF
  • Method Overloading (Ploymorphism - )
========================================================================
Basic Java Program

Java: java is an object-oriented programming language originally developed by Sun Microsystems and release in 1995. Which was originally developed by James Gosling at Sun Microsystems.
In order to work with java we have to write the code which we will be calling as source code. So we have to create a file with extension as .java and this file is known as java file.
So in order to write the code we have to first declare a class with the keyword "class". See java has given us lot of reserved keywords which we will using further to work with Java.
class will be followed by curly braces'es.

Class is a collection of similar type of object which include data members (variables ) or member function (methods).

Remember java is a case sensitive language.



Now As you can see we have a line of code as "public static void main (String[] args)". this is a method given by Java to run the lines of codes writing in the block of this function.

Lines which will be written in Main method will only be run by java.

In java to print anything to the console we have to use "System.out.println" method.

So in the above program we want to print "I Rule" text on the console.

In order to see the output we have compile the java source code and then run the program see below :

Why Java Is Platform Independent

In order to understand portability in Java you need to understand what happens to java code from start to finish.

  1. Source Code (Written by Developer/QA) (Machine Neutral).
  2. Compiled Code / Byte Code (Compiled by javac) (Machine Neutral).
  3. Byte Code executed (Executed by JVM) (Machine Specific) 

Javac (Java Compiler) converts Java code to byte code. This can be moved to any machine (Windows / Linux) and executed by JVM. JVM reads the bytecode and generates machine specific code. In order to generate machine specific code JVM needs to be machine specific. So every type of Machine (Windows / Linux / Mac) has a specific JVM. So in this way the coder doesn’t need to bother with generating byte code. JVM takes care of portability. So the final answer is Java is Portable but JVM is Machine Specific.
Basic Convention

  1. Name of the class should start with capital letter.
  2. Name of the package, data-members, member function should start with small letters.
  3. Name of the java file should be same as class name having main method.

Data Types in java

In Java data types are categorized in two categories

  1. Primitive Data Types
  2. Non-Primitive Data Types

Primitive Data Type can be further divide in to eight types i.e. byte, short, int, long, char, float, double, and Boolean.

Non Primitive Data Types are Class Interface and array.

lets start with primitive data type in later blog will see how to work with Non-Primitive data types.

Primitive data types can be further categories in four categories as define below.




In Java variable can be defined in different places i.e.

Variables :- Variables are the name of reserved area in Java.
  1. variables can be defined in a class - these are called class level variable or global variables.
  2. variables can be defined in a methods - these are called local variables.
  3. Variables can be defined in method declaration - these are called parameters.

Example with declaration of Variables

1. Global Variable or Class Level Variable



Note:- 

  • In above example we have only instance variable.
  • Variable which are not static are known as instance.
  • Variable which are defined at class level without static keyword are know as instance variable.
  • Class level instance variable can only be used with the help of object.
  • Class level variable may or may not be initialize.
  • Scope of class level variable is throughout the class.
  • At class level java define default value to a variable based on the type.

2. Local Variable.



Note

  • Local variables are defined with in a function and scope of that variable is limited to only that function.
  • Local variable can be declared without any value, however they must be initialize before they use.
    • Refer below screen print


3. As A Parameter


Notes:-

While calling function having parameter in main method, we have to call as per the declaration i.e we function is having single parameter then we need to pass the value for that parameter based on the declaration type (As shown in the above screen print).

What Is Meant Instance Variable or Function

Instance variables are used by Objects to store their states. Variables which are defined without the STATIC keyword and are Outside any method declaration are Object specific and are known as instance variables as we have seen earlier.

Same is applicable with Methods as well in java all the methods which are defined without static keyword are know as instance methods and they can be called with the help of instance of a class (object).

Example of Instance Variable and Instance function 





Static Keyword In Java

The static keyword in java is used for memory management mainly. We can use java static keyword with variables, methods and classes. 

Note all the static Data Members and Member Function are belong to class.

So if we don't want to create an object or we can say if we don't want to instantiate an class to call the functions (Member Functions) or variables (Data Members) then we need to declare those function or variables as Static.

Example of Static Variable and Static function 

To define a variable or a function we have to specify the static keyword, please have a look to below screen shot.


Note:-
  1. Static keyword is use to define static data members or member function.
  2. Static data members and member function can be accessed in three ways. (Via object, via class, directly)
  3. Static variables are always declare at class level.
  4. Static variable means a property i.e. shared with all the object of class.
  5. Remember static function should always be accessed in static way i.e with in class static member of function should be accessed directly if we want to use these variables and methods then we can call these using class name.
  6. An instance variable is one whose memory space is creating each and every time whenever an object is created.
  7. For static variables memory space is created only once when the class is loaded by class loader subsystem (a part of JVM) in the main memory irrespective of number of objects.
  8. Value of instance variable is not shareable.
  9. Value of static variable is always recommended for shareable.
  10. Instance variable are also known as object level data members since they are dependent on objects.

Please refer below examples to understand points from 6 to 10.

Example 1



Example 2:-


Basic Convention/Rules for Data Types



  1. All primitive data members are signed i.e. positive and negative.
  2. All variable names must begin with a letter of the alphabet, an underscore, or ( _ ), or a dollar sign ($).  The convention is to always use a letter of the alphabet.  The dollar sign and the underscore are discouraged.
  3. The name can be of any length, but don't get carried away.  Remember that you will have to type this name.
  4. Uppercase characters are distinct from lowercase characters.  Using ALL uppercase letters are primarily used to identify constant variables.  Remember that variable names are case-sensitive.
  5. You cannot use a java keyword (reserved word) for a variable name.
  6. Variable names are case-sensitive.
  7. At class level java define default value to a variable based on the type.
  8. Local variable must be initialize before they used.

Please have a look to reserved keywords in Java



Method Overloading In Java

In simple terms we can say that a class can have more than one methods with same name, but with different number of arguments/ parameters.


These parameters may be different on two bases:
1) Parameter type: if the type of the parameter passed in the function are different.
2) Parameter count: if the number of parameter passed in the function are different.

Note: you can also achieve overloading by changing the sequence of parameters.

Method overloading take place in the same class only.

Example 




Note:-

  • We can't achieve method overloading by changing in return type of a method or by changing the access modifier and specifier.
    • However there is an exception that after java 1.5 we can achieve the method overloading by changing the return type only (we will see this in further session (Day-3)).
  • Main() is also an method and we can overload the main method as well.
  • System.out.println() method is by default overloaded in Java.




========================================================================

Links for the files (.Java File)

https://drive.google.com/drive/folders/0B5M4BcbfVN07RG5HSVBXdGdXWWc?usp=sharing


Links for recorded Video


https://www.youtube.com/watch?v=xfOXObLcN-Q&feature=youtu.be


========================================================================








  1. bharat varshney said...

    nice explanation!!!

  2. Tarun Kumar said...

    Very crisp and clear explanation!

  3. Unknown said...

    very much informative and understandable

Post a Comment