Saturday, July 14, 2007

Java hints ...

I have started learning Java, I am going to leave my hints from SAMS teach yourself Java in 24 hours here in my blog.

Hope it helps someone ...

Java programs that run locally on your own computer are called applications. Programs that run on web pages are called applets.

Unlike applications, applets do not have a main() block. Instead, they have several different sections that are handled depending on what is happening in the applet. Two of the sections are the init() block statement and the paint() block. init() is short for initialization, and it is used to take care of anything that needs to be set up as an applet first runs. The paint() block is used to display anything that should be displayed.

Java is case-sensitive when it comes to variable names, so you must always capitalize variable names in the same way throughout a program.

The first letter should be lowercase, and if the variable name has more than one word, make the first letter of each subsequent word a capital letter. For instance, if you wanted to create an integer variable to store the all-time high score in a game program, you can use the following statement : int allTimeHighScore;

You can't use punctuation marks or spaces in a variable name, so neither of the following will work:

int all-TimeHigh Score;
int all Time High Score;

The following order is used when working out an expression:

1. Incrementing and decrementing take place first.

2. Multiplication, division, and modulus division occur next.

3. Addition and subtraction follow.

4. Comparisons take place next.

5. The equal sign (=) is used to set a variable's value.

In Java, a long integer variable can be anything from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. This range ought to give your mathematical expressions plenty of breathing room when you can't use int, which has a range of –2,147,483,648 to 2,147,483,647.

Unlike the other types of variables you have used—int, float, char, boolean, and so on—the name of the String type is capitalized. The reason for this is that strings are somewhat different than the other variable types in Java. Strings are a special data resource called objects, and the types of all objects are capitalized.

To display double quotations in a string, Java has created a special code that can be put into a string: \". Whenever this code is encountered in a string, it is replaced with a double quotation mark. For example, examine the following:
System.out.println("Jane Campion directed \"The Piano\" in 1993.");

You can insert several special characters into a string in this manner. The following list shows these special characters; note that each is preceded by a backslash (\).

Special characters

Display

\'

Single quotation mark

\"

Double quotation mark

\\

Backslash

\t

Tab

\b

Backspace

\r

Carriage return

\f

Formfeed

\n

Newline

The + operator has a different meaning in relation to strings. Instead of trying to do some math, it pastes two strings together. This action can cause strings to be displayed together, or it can make one big string out of two smaller ones.Concatenation is a word used to describe this action, because it means to link two things together.

 
 
favorite.equals(guess)

This code statement is known as a method. A method is a way to accomplish a task in a Java program. This method's task is to determine whether one string, favorite, has the same value as another string, guess. If the two string variables have the same value, the text TRue will be displayed. If not, the text false will be displayed. The following is the output of this example:
 
String change = baines.toUpperCase();

int nameLength = cinematographer.length();
 

To look inside a string, use its indexOf() method. Put the string you are looking for inside the parentheses. If the string is not found, indexOf() produces the value -1. If the string is found, indexOf() produces an integer that represents the position where the string begins. Positions in a string are numbered upwards from 0, beginning with the first character in the string. (In the string "The Piano", the text "Piano" begins at position 4.)

Script is the string that includes the whole text.

int position = script.indexOf("you have shamed those trunks");

The indexOf() method is case sensitive, which means that it only looks for text capitalized exactly like the search string. If the string contains the same text capitalized differently, indexOf() produces the value -1.

To be continued ...