Saturday, May 19, 2007

Primitive Data Types

Java is Strongly typed. meaning is we have to declare the variable before we can use them.

e.g. double speed=30;

Above statement in a code indicates that field named 'speed' exists and holds a numeric value.Fields data type determines what kind of value it will hold (whole number/with fractional part etc.)like 'int' will have only whole number whereas double (in above example) can have fractional part as well.

Apart from int,double there are 6 more data types in Java.Let's go into the details of all one by one.

1) byte
8-bit Signed Integer
Minimum Value = -128 (Inclusive)
Maximum Value = 127 (Inclusive)
Negative numbers are stored in 2's complement form.

2)short
16-bit Signed Integer
Minimum Value = -32,768(Inclusive)
Maximum Value = 32,767 (Inclusive)
Negative numbers are stored in 2's complement form.

We can use a 'byte' and 'short' to save memory in large arrays (if we know that value will not cross maximum & minimum limit), in situations where the memory savings actually matters.

3)int
32-bit Signed Integer
Minimum Value = -2,147,483,648(Inclusive)
Maximum Value = 2,147,483,647(Inclusive)
Negative numbers are stored in 2's complement form.


4)long
64-bit Signed Integer
Minimum Value = -9,223,372,036,854,775,808 (Inclusive)
Maximum Value = -9,223,372,036,854,775,807 (Inclusive)
Negative numbers are stored in 2's complement form.


5)float
single-precision 32-bit IEEE 754 floating point


6)double
single-precision 64-bit IEEE 754 floating point

As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers.



7)boolean
Represents one bit of information.Only two possible values true and false.
Usage of this data type is as a simple flags to track true/false conditions.
It's size is not defined precisely


8)char
single 16-bit Unicode character
Minimum Value = '\u0000' (Inclusive)
maximum Value = '\uffff' (Inclusive)

Java also provides special support for character strings via the 'String' class.
Although String is not a Primitive Data type.
for example,
String s = "this is a simple string";

String objects are immutable, which means that once created, their values cannot be changed.

Default Values of Primitive Data Types

Fields (class level data members) that are declared but not initialized will have some default values. These values will be assigned by the compiler. These default values are either Zero ('0') or null.

For byte,short, int default value is '0'.
For long it id '0L'
For Float it's 0.0f,whereas default value for double is 0.0
char will have '\u0000' as a default value whereas String (or any other object for that matter) has 'null' as its default value.
boolean has 'false' as it's default value.

Note:
1) Local variables are never initialized by the compiler.
2) As a programmer, we have to initialize a local variable where it is declared.
3) If we try to access an uninitialized local variable , we will get a compile time error.

Saturday, May 5, 2007

Java Language Basics - Conventions used for Naming Variables

Every programming language has its own set of rules and conventions for the kinds of names that you're allowed to use.The rules and conventions for naming your variables as far as Java is concerned are given below:

1.Variable names are case-sensitive

2.A variable's name can be an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_".
Note: Normally the convention is to start a variable name with letter & not with $ or underscore (_) character.

3.White space in variable name is not permitted.

4.Subsequent characters after first letter may be letters, digits, dollar signs, or underscore characters

5. Use full words instead of cryptic abbreviations; this will make your code easier to read and understand.
e.g. use variable name as 'speed' instead of 's' etc..

6.Variable name can't be standard keywords/reserved words
Visit the following Link to see the list of Java Keywords & Reserved words
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html

7.If variable name is a single word name that in lowercase
e.g. int speed;

8.If a variable name has more than one word, then start with lowercase and then capitalize the first letter of each subsequent word
e.g. int numberOfGears; or double capitalPrice;

9.If your variable stores a constant value (in Java constants are marked with attaching a 'final' keyword before variable name)such as
static final int NUMBER_OF_GEARS = 6;
(NUMBER_OF_GEARS is a constant static class variable whose value can't be changed)

the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.


In the next post we will have a look at "Primitive Data Types" in Java.

Friday, May 4, 2007

Java Language Basics - Variables

In this and subsequent posts we will try to understand very basic concepts in Java Programming Language namely,

Variables
Conventions used for Naming Variables
Primitive Data Types
Arrays
Operators
Expressions,Statements and Blocks
Control Flow Statement

Those who are already aware of C,C++ languages will be knowing most of the stuff, However there could be few things you may learn which are java specific in this post.
So, I would recommend them to read this post as well.


Let's begin

Varaibles

As we have seen earlier, an object stores its state in fields.In the Java programming language, the terms "field" and "variable" are both used; this is a common source of confusion among new developers, since both often seem to refer to the same thing.

The Java programming language defines the following types of variables:

Instance Variables (Non-Static Fields)
Class Variables (Static Fields)
Parameters
Local Variables

Instance Variables (Non-Static Fields)
Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words);
e.g. 'Speed' variable is applicable to each car and this variable will decide the state of an object 'Car'. So In general, objects store their individual states in "non-static fields".

Class Variables (Static Fields)
A class variable is any field declared with the "static" keyword; this tells the compiler that there will be exactly one copy of this variable in existence, regardless of how many instances of this class are creted.
e.g. Number of Gears for specific model of a car will remain same acoross all the models so we could define it as static. i.e. static int numberOfGears =5;

Parameters
We've already seen examples of parameters, Signature for the main method is public static void main(String[] args) Here, the args variable is the parameter to this method.The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well like constructors etc. which we will see later.

Local Variables
Local variable are declared between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
e.g. int count=10;
There is no special keyword which will distinguish them as local, they are used by the methods in which they are declared to store their temporary state

It's always recommened that, you initialize the local variable where you declare it.


In the next post we will focus on "Conventions used for Naming Variables".

Till then bye bye

Saturday, April 14, 2007

Java Training

We will be Starting Java Training shortly......

Friday, March 9, 2007

My Voice

You could share your questions on this blog by writing comments to related post.

I would definitely add those in a new post. I would give the answers for the question if it is unanswered.....

Wednesday, March 7, 2007

FAQs

Please find below the list of few Frequently Asked Questions (FAQs) regarding the topic we have covered so far....


What is abstraction?
What is encapsulation?
What is the difference between an object and a class?
What is polymorphism? Explain with an example?
What is inheritance?
What is an object?
What is a class?

Interviewer could give you an object example & would ask you to list its State & Behavior.

For C++ Programmers:

What is the difference between structures and classes in C++?

For C Programmers:

What is the difference between structures in C and structures in C++?


For most of the questions answers are available in below posts .
For others (specific to C, C++) you should take some effort to find out the answer.

Will catch you later......

Monday, March 5, 2007

Open Secret

The title of the post is intentionally non technical ........and it has some meaning....How?The reason is quite simple....as the name suggests .......you already know the stuff that will be presented in the current post.....only difference would be we will look this entire stuff from different view point.....

This post exclusively deals with answer asked in the last part of the previous post.

We will be thinking of one real world entity (object) & we will try to find out all the major pillars of OOPs in it..which will be a proof of concept for the discussion we had in earlier post.Ready? Get...Set...Go.....

I am thinking of Personal Computer as real world entity.Now biggest question is how Abstraction, Encapsulation, Inheritance , Polymorphism and Modularity applies to this object?

I am sure you must have considered other objects as well & must have done some research on this...right?Personal Computer (PC) as an object is a very good example to understand these major pillars.

If i have to purchase a PC , very obvious step i would take is i will consult my friends, relatives and colleagues who know about it. I am sure they will ask me for What reason you are purchasing it...meaning is what you are going to do with that? or How you will make use of PC? isn't it?If i say, I am going to use for my personal use most of them would suggest me some configuration (Memory Size, CD Drive, Speakers etc..)If i say , I am going to use this as a high end server in my office or wherever this configuration would change (bit obvious that there would be more memory, speakers won't be there.)If i am going to gift this PC to a School for some educational purpose , the changes in configuration would be more multimedia features (to Show Educational CDs) and may be more multimedia related software (which were missing when i am supposed to use PC as a High End Server)

In these 3 examples i am sure you will appreciate the fact that, PC remains the PC ....Object is same , however we changed the configuration i.e. attributes of PC object according to our need and usability. We selected few attributes and dropped few one as usability changes, This is Abstraction. How we define Abstraction:Abstraction refers to identifying key aspects (and obviously ignoring/ dropping the rest) of an object/entity for the specific application.

Now you know why the title of this post is "Open Secret "............

Can i consider various electronic components on motherboard of computer or in general PC as a data members and the way we can operate the PC as a behavior of PC?Let me explain....I am a user ....I could start the PC as well as i could shutdown the PC ...when i do this there is some typical behavior shown by PC... right?......this is behavior of PC as an object......and i could do much more than this with PC...When i turn PC on / off do you agree that electronic components behind the scenes are at work..(Some capacitor gets charged and discharged & so on...)....This is Encapsulation...How Encapsulation is defined:Encapsulation is a process where data(attributes) & methods (behavior) is rolled into a single entity known as object. It protects data within an object & ensures that it could be accessed only by its methods.As a user i never need to understand the details of how PC operates but to know how i should operate it or what are the possible operations i could do with PC?...Hiding the complex details of an object is an aspect of Encapsulation.I turn on & off the PC and do some other stuff (behavior of the PC) as well and behind the scenes many Electronics parts of PC undergo electrical change(change in attributes).And end user never knows these details. This is Encapsulation

I am sure everybody is aware of PCs with Intel's Pentium IV processor , what was there earlier to Pentium IV processors? Pentium III ....earlier to this ? Pentium II .....earlier to this? Pentium I/Pentium in general.......prior to that 80486...80386....80286.........80186.......8086...8085 (In early 70's)...so on...So each time new processor for PC comes to market there are few enhancements....No need to think that new Processor is 100% brand new...it has same structure / architecture of the previous processor and there is something to more to it in new version e.g. 80186 is-a 8086 but there were few enhancements compared to 8086...so we normally use the term code written on 80186 is "backward-compatible" with 8086 & so on for other processors ..isn't it?This backward-compatibility is achieved because some part in older processor is carried forward to next processor & there are advancements on top of it.I am talking about Inheritance (Definition: Inheritance basically refers to hierarchy to reduce complexity) So if you are following the posts then, i would simply say that,Pentium IV is Subclass of Pentium III .....Let me know....whether this statement is true?

I am repeating the part of my earlier post to explain Polymorphism Answer these simple questions (you can do the steps side by side to have a fun) at the end we will revisit the definition Do you use mouse while using your PC?Have you ever used a right click button of a mouse?Have you right clicked on Files, Folders, Desktop etc..?Have you observed that response (pop-up contents) you get when you right click on Files , Folders and Desktop is different ? ...If not please check it now....Sure they are different?Hey, you have now understood Polymorphism....nothing great right?Definition : It is the ability of different objects to respond to the same message differently. Ability of different objects (Files, Folders, Desktop etc..) to respond to the same message (right click) differently(different contents on pop-up) is Polymorphism.

PC is composed of many loosely coupled components (they are object within themselves)...so different components like Keyboard, Mouse, Motherboard, Monitor, connectors and cables constitute the whole object PC however these are loosely coupled so that if keyboard or mouse or anything else is not working properly you can easily replace them individually without affecting other components say While replacing Keyboard you don't have to do anything with Monitor or motherboard etc...This is an example of Modularity (In OOPs modularity refers to breaking up the system in small modules which are loosely coupled to each other & act as a single system or as a Cohesive system. This way it is easy yo maintain & easy to extend) in case of a PC....


This brings me to end of this post ... I hope you have enjoyed the journey so far.........

Stay tuned for more........
 
online degree programs
visit us .