Learn Java - Part 2

 
 Let`s start with Java Basic Concepts :


Before moving for java programming we must understand the basic concepts of the Java Language.Most Java programs use a collection of objects of many different types. Such as Class, Object , State , Behavior. Let`s looking in to each of them listed above.





Class : A template that describes the kinds of state and behavior that objects of its type support.

Object :At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class.

State(instance variables) : Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state.

Behavior(methods) : When a programmer creates a class, she creates methods for that class. Methods are where the class' logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.

What are Java Identifiers..

 
All the Java components that we are talking about(classes, variables, methods.. etc) need name to identify them uniquely. In java those names are called identifiers.

What are Legal Identifiers...
Technically, legal identifiers must be composed of only Unicode characters, numbers, currency symbols, and connecting characters (like underscores). The exam doesn't dive into the details of which ranges of the Unicode character set are considered to qualify as letters and digits. So, for example, you won't need to know that Tibetan digits range from \u0420 to \u0f29. Here are the rules you do need to know:

■ Identifiers must start with a letter, a currency character ($), or a connecting character such as the     underscore ( _ ). Identifiers cannot start with a number!

■ After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers.

■ In practice, there is no limit to the number of characters an identifier can contain.

■ You can't use a Java keyword as an identifier. Table 1-1 lists all of the Java keywords including one new one for 5.0, enum.

■ Identifiers in Java are case-sensitive; foo and FOO are two different identifiers.

Examples of legal and illegal identifiers follow, first some legal identifiers:

int _a;
int $c;
int ______2_w;
int _$;
int this_is_a_very_detailed_name_for_an_identifier;

The following are illegal (it's your job to recognize why):

int :b;
int -d;
int e#;
int .f;
int 7g;

What are Java Keywords..

Like all programming languages, Java has a set of built-in keywords. These keywords must not be used as identifiers (names of the Java Components). 

Below i have listed many of the available Java Keywords.
abstractcontinuefornewswitch
assert***defaultgoto*packagesynchronized
booleandoifprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthrows
caseenum****instanceofreturntransient
catchextendsintshorttry
charfinalinterfacestaticvoid
classfinallylongstrictfp**volatile
const*floatnativesuperwhile
*not used
**added in 1.2
***added in 1.4
****added in 5.0

As well as if we are going to be a good Java Programmer we must adder to the Oracle Java Coding Conventions. That means all of the names(Identifiers) should follow the its naming conventions. It is not a must, but as programmers we must follow it.

Java Naming Conventions..

When choosing a name for an identifier make sure it's meaningful. For instance, if your program deals with customer accounts then choose names that make sense to dealing with customers and their accounts (e.g., customerName, accountDetails). Don't worry about the length of the name. A longer name that sums up the identifier perfectly is preferable to a shorter name that might be quick to type but ambiguous.

A Few Words About Cases..
Using the right letter case is the key to following a naming convention.
  • Lower Case is the wher all the letters in a word are written without any caoitalization(eg : while,  if, mypackage)
  • Uppercase is where all the letters in a word are written in capitals. When there are more than two words in the name use underscores to separate them (e.g., MAX_HOURS,  FIRST_DAY_OF_WEEK).
  • Camel Case  (also known as Upper CamelCase) is where each new word begins with a capital letter (e.g., CamelCase, CustomerAccount, PlayingCard).
  • Mixed Case (also known as Lower CamelCase) is the same as CamelCase except the first letter of the name is in lowercase (e.g., hasChildren, customerFirstName, customerLastName).
Java Naming Conventions..


  • Packages: Names should be in lowercase. With small projects that only have a few packages it's okay to just give them simple (but meaningful!) names:
     package pokeranalyzer
     package mycalculator 
    In software companies and large projects where the packages might be imported into other classes, the names will normally be subdivided. Typically this will start with the company domain before being split into layers or features:
     package com.mycompany.utilities
     package org.bobscompany.application.userinterface 
  • Classes: Names should be in CamelCase. Try to use nouns because a class is normally representing something in the real world:
     class Customer
     class Account 
  • Interfaces: Names should be in CamelCase. They tend to have a name that describes an operation that a class can do:
     interface Comparable
     interface Enumerable 
    Note that some programmers like to distinguish interfaces by beginning the name with an "I":
     interface IComparable
     interface IEnumerable 
  • Methods: Names should be in mixed case. Use verbs to describe what the method does:
     void calculateTax()
     string getSurname() 
  • Variables: Names should be in mixed case. The names should represent what the value of the variable represents:
     string firstName
     int orderNumber 
    Only use very short names when the variables are short lived, such as in for loops:
     for (int i=0; i<20;i++)
     {
        //i only lives in here
     } 
  • Constants: Names should be in uppercase.
     static final int DEFAULT_WIDTH
     static final int MAX_HEIGHT 
  • 3 comments :

    superb work bro..!!

    August 8, 2013 at 3:06 AM comment-delete

    Thnx Bro

    August 11, 2013 at 11:03 AM comment-delete

    So many good posts. liked it! We know how much important java is for a developer. That's why we bring much more details for you. just take a look java basic concepts

    December 1, 2014 at 3:14 AM comment-delete

    Post a Comment