Thursday, August 4, 2011

Conditions and Conditional Operators

Numerical Comparison Operators






















































































































































OperatorSyntax Description
<a < bResult is true if a is less than b; otherwise, it is false.
<=a <= bResult is true if a is less than or equal to b; otherwise, it is false.
>a>bResult is true if a is greater than b; otherwise, it is false.
>=a>=bResult is true if a is greater than or equal to b; otherwise, it is false.

Boolean Logical Operators








































































































































































































































































































































































































































































































































































































































































































































































OperatorDescriptionExamplesResult
&&Conditional -ANDfalse && falsefalse
false && truefalse
true && falsefalse
true && truetrue
Conditional -ORfalse falsefalse
false truetrue
true falsetrue
true truetrue
&Logical-ANDfalse & falsefalse
false & truefalse
true & falsefalse
true & truetrue
Logical-ORfalse falsefalse
false truetrue
true falsetrue
true truetrue
^Logical -XORfalse^falsefalse
false^truetrue
true^falsetrue
true^truefalse



Compound Assignment Operators:









































































































Operator





Description





Example





Same Result as





+=





Increment assignment operator





x += 5;





x = x + 5;





–=





Decrement assignment operator





x –= 5;





x = x – 5;





*=





Multiplication assignment operator





x *= 5;





x = x * 5;





/=





Division assignment operator





x /= 5;





x = x / 5;





%=





Modulus assignment operator





x %= 5;





x = x % 5;


Basic if , if-else, switch statements

if Statement

Syntax:

if (condition)
{
...
java statements;
...
}

Example:

// == results in a Boolean value true or false
if (RandomNumber % 2 == 0)
{
System.out.println("Even");
}


If-Else Statement

Syntax:

if (condition)
{
java_statements_for_true_condition;
}
else
{
java_statements_for_false_condition;
}

Example :

if (a % 2 == 0)
{
System.out.println("even");
}
else
{
System.out.println("odd");
}

You can eliminate the else statement when using the Boolean variable.

Example:

Typical if-else statement:

if (hungry)
{
System.out.printIn (" Go find food !! ")
}
else
{
System.out.printIn (" Continue working !! ")
}

Alternatively to eliminate the else statement by using the not operator

if (!hungry)
{
System.out.printIn (" Continue working !! ")
}

Switch Conditional Statement

Syntax:

switch (expression) {
case value1:
statements_for_value1;
break;
case value2:
statements_for_value2;
break;
case value3:
statements_for_value3;
break;
default:
statements_for_any_other_value;
}

Example:
switch (n)
{
Case 1:
System.out.printIn("I miss you!!");
break;
Case 2:
System.out.printIn("I care for you!!");
break;
Case 3:
System.out.printIn("I love you!!");
break;
default:
System.out.printIn("Best friends forever!!");
}

Wednesday, August 3, 2011

First Java Applet

(1) Create a file JavaProgramming.java with the source code below and compile it.

Source Code:

/*
*JavaProgramming
*Applet
*/

//imports the graphics class
import java.awt.Graphics;

//extends class characteristics and capabilities
public class JavaProgramming extends java.applet.Applet {

public void paint(Graphics g)
{
//tells the applet to print out the string quoted in a specified (x=10,y=50)
coordinate
g.drawString("Welcome to Java Programming!!" ,10, 50);
}

}

(2) In order to run the Applet, you would need to write HTML doc and include the applet within it.Alternatively used the sample below and save it as JavaProgramming.html.

Source Code:



(3)To run the applet. Type the following command prompt:

appletviewer JavaProgramming.html

Congratulations!! You've just run your first basic Applet.

Tuesday, August 2, 2011

Creating First Java Application

Basic steps to creating your first Java Application:

(1) Create a file name JavaProgramming.java. To do this, you can start by using Notepad and write your own source code or alternatively copy the source code below and save it. ( Note that the file name is case-sensitive)

(2) Sample source code:

/*
* Welcome to Java Programming
*/

//class definition
public class JavaProgramming {

//main method definition
public static void main(String args[])
{

//print out Welcome to Java Programming!! Big Achievement!!
System.out.println("Welcome to Java Programming!! Big Achievement!!");
}

}

(3) Before you run any Java Application, you would need to compile the source code by using javac command.For example at your operating system’s command prompt (such as the DOS prompt), type javac JavaProgramming.java


If you face any problems such as



'javac' is not recognised as an internal or external command, operable program or batch file. This is a commanly known issue as you may have not specify the PATH correctly during installation.Basically,you would have to find where you have installed the jdk(version no.) file and the bin directory and execute the command.

Eg:
C:\Java\jdk7.0.1\bin>javac JavaProgramming.java



Once completed, you have successfully converted your source code into Java byte code.The file extension for Java byte code is .class. Thus, you should see a new file named HelloWorld.class created by the Java compiler in the same directory as the source code file.

Final step is to run the file. To do this, just type java JavaProgramming

And you should be able to see the screen shot below:



Congratulations!! You have successfully created your first Java Program !!

Monday, August 1, 2011

Windows Installation for JAVA SDK/SRE

Open Source Software Downloads Sites:

Before you start learning Java Programming make sure that you have downloaded the latest version of the SDK/JDK as well as the JRE from

(1) Sun Website:

http://java.sun.com/j2se/1.3/docs.html.

or

(2) Oracle Website:
http://www.oracle.com


Windows Installation and Setup:

Follow these instructions if you are running Windows operating systems(OS).

(1) Run the SDK installer program. If you install JDK in the default directory, a folder named jdk(version no.) will be created on your C drive.

(2) Update the PATH variable. The PATH variable tells your system which directories to look in when running commands or .EXE files. Setting the PATH variable allows you to easily compile and run your Java programs from any directory. Before you do this, you need to verify where your SDK’s bin folder is. If you used the default installation directory, the path is C:\jdk(version no.)\bin. If you installed to a different directory, you need to find the jdk(version no.) directory, which will contain the bin directory.

Basic Java Data Types

Primitive Data Types :
They are variables which are built into the system. Note that they are not objects.
Having to declare a variable of a specific primitive data types it would always hold the value of the data type. For example, if you were to declare a as a float, a would always be holding a float value.There's a list of eight independent primitive data types in java shown in table below.



























































































































































































































































































































































Keyword












Description












Size



byte



Byte-size
integer












8-bit












short












Short
integer












16-bit












int












Integer












32-bit












long












Long
integer












64-bit












char












Single
character












16-bit












float












Single-precision
floating point












32-bit












double












Double-precision
floating point












64-bit












boolean












True
or false












1-bit




Literals :
Literals specifies a specific data value. You can easiily manipulate the datas - whatever things you assign to a variable that is what you get. For example, 8 is typically an int value. However you can assign this as a double and it would be treated as a double.
































































































































































































Literal









Data Type









27, 027,0x1c









int









100L









long









45.0D , 27.0e3









double









99.0F









float









'B'









char









true /false









boolean









null









null object reference



Mathematical Operators:














































Operator




Description




+




Addition




-




Subtraction




*




Multiplication




/




Division




%




Modulus (division remainder)





(Note :

- Modulus operator works by dividing the number on the left side by the number on the right side evenly and giving the remainder. For example; if you were to divide 9 by 2 , it gives you the remainder 1

-Just like mathematics,operator precedence takes place whenever you use these mathematical operators. It determines the order in which operations are applied to numbers.(eg: *,/,% have precedence over + and -)