JAVA Beginners part 4

Overriding in simple sense: It means the subclass that inherits the super class redefines the inherited method whenever it needs.

Classes and Objects.

While creating a class think of an Object which will be created out of it.

t2

Instance variables is the data represent the object.Methods are the functions that an Object can do. These Objects and Methods are defined as a part of Class.

Class is a Blueprint of an Object. Class tells JVM this is how to create an Object of this type.Each object can have its own values.

My First Object 

create and test. We would need two classes , one is the class i am creating and then is tester class which contains the main method and in that main method we will test the methods and variables of newly created class.

this mind sound like a recipe, bear with me.

Why do we need Main method

It is the starting point of execution. or you could say start of your JAVA app.

Hint of JAVA garbage collection.

All the objects that you create in the program are saved in something called as Heap by the JVM. JVM allocates the memory space in Heap based on the instance variables the Object is having etc, for example Object with 20 variables would have more space than the 5.

Now. What is Garbage collection, while you might have an idea with the literal meaning. It is JAVA memory management process. JVM automatically clears the objects that are no longer used or referenced in the program.

j3

Taking a peek into future

You say all code in Java is written in class files. So if i have a big Java application then there would be lot of files. Wouldn’t that be a pain to deliver all those files onto client machine?

Well, yes. We have a solution for it. All the application files can be bundled into a .JAR File that’s based on pkzip format. Inside the JAR there can be a file called manifest which will give info of which file holds the main method that can run the App.

You see all these things simple very obvious and yet when you are at an Interview you will be like, that is so simple why the hell i cant remember that.

There is a very good reason for it. I had learnt this from a Memory master, that which ever thing ( idea/object/anything) which seems obvious to you, it is highly likely you will forget that the most.

Duncan J watts says that our ability to classify things as obvious impedes our ability to understand and learn that. 

Boolean test = true; // this is how you define Boolean variables. It can take only two values.

t4

Above is an example of the access level. Not exactly though.

Variables

There are two types of variables. Primitive( Int , Boolean etc) and Object References (Holds references to objects)

how to declare a variable, type and the name ex: int count;

Famous primitive types:
boolean 1 bit default = false
char 16 bits default = \u0000
byte 8 bits (numeric,integer)
short 16 bits (numeric,integer)
int 32 bits 4 bytes (numeric,integer)
long 64 bits 8 bytes (numeric,integer)
float 32 bits (numeric,float)
double 64 bits (numeric,float)

Ponder.

Max value that you can hold in a Integer
Integer.MAX_VALUE, which is 2^31 – 1 = 2147483647.
value 255 is stored as (11111111) base 2. What that means is you need 8 bits to represent 255 , so if you have 32 bits roughly you can represent a 10 digits number.

float f = 32.5f
note the f . if you dont put f then JAVA will treat it as double

Few naming rules for variables

can = start with a letter, underscore
cannot = number, keywords

 

References:

Head First Java 2nd Edition

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s