When we consider a Java program it can be defined as a collection of
objects that communicate via invoking each other's methods. Let us now
briefly look into what do class, object, methods and instance variables
mean.
public class ArrayExamples
{
- Object - Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.
- Class - A class can be defined as a template/ blue print that describes the behaviors/states that object of its type support.
- Methods - A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
- Instance Variables - Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.
First Java Program:
public class MyFirstJavaProgram
{
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args)
{
System.out.println("Hello World"); // prints Hello World
}
}
Let's look at how to save the file, compile and run the program. Please follow the steps given below:
- Open notepad and add the code as above.
- Save the file as: MyFirstJavaProgram.java.
- Open a command prompt window and go o the directory where you saved the class. Assume it's C:\.
- Type ' javac MyFirstJavaProgram.java ' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption : The path variable is set).
- Now, type ' java MyFirstJavaProgram ' to run your program.
- You will be able to see ' Hello World ' printed on the window.
Hello World Program:
HelloWorld.java
public class HelloWorld
{
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
public class HelloWorld
{
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
CallingMethods :
A sample of how to call methods in the same class.
CallingMethodsInSameClass.java
Illustrates how to call static methods a class from a method in the same class
public class CallingMethodsInSameClass
{
public static void main(String[] args) {
printOne();
printOne();
printTwo();
}
Illustrates how to call static methods a class from a method in the same class
public class CallingMethodsInSameClass
{
public static void main(String[] args) {
printOne();
printOne();
printTwo();
}
public static void printOne() {
System.out.println("Hello World");
}
System.out.println("Hello World");
}
public static void printTwo() {
printOne();
printOne();
}
}
printOne();
printOne();
}
}
For Loop :
A simple example of using for loops to calculate factorial. Uses the built
in int data type so only good to 13!
public class Factorial
{
public static void main(String[] args)
{ final int NUM_FACTS = 100;
for(int i = 0; i < NUM_FACTS; i++)
System.out.println( i + "! is " + factorial(i));
}
public static int factorial(int n)
{ int result = 1;
for(int i = 2; i <= n; i++)
result *= i;
return result;
}
}
{
public static void main(String[] args)
{ final int NUM_FACTS = 100;
for(int i = 0; i < NUM_FACTS; i++)
System.out.println( i + "! is " + factorial(i));
}
public static int factorial(int n)
{ int result = 1;
for(int i = 2; i <= n; i++)
result *= i;
return result;
}
}
Value Parameters :
An example that shows the behavior of value parameters. In
Java all parameters are passed by value.
public class PrimitiveParameters
{
public static void main(String[] args)
{ go();
}
public static void go()
{ int x = 3;
int y = 2;
System.out.println("In method go. x: " + x + " y: " + y);
falseSwap(x,y);
System.out.println("in method go. x: " + x + " y: " + y);
moreParameters(x,y);
System.out.println("in method go. x: " + x + " y: " + y);
}
public static void falseSwap(int x, int y)
{ System.out.println("in method falseSwap. x: " + x + " y: " + y);
int temp = x;
x = y;
y = temp;
System.out.println("in method falseSwap. x: " + x + " y: " + y);
}
public static void moreParameters(int a, int b)
{ System.out.println("in method moreParameters. a: " + a + " b: " + b);
a = a * b;
b = 12;
System.out.println("in method moreParameters. a: " + a + " b: " + b);
falseSwap(b,a);
System.out.println("in method moreParameters. a: " + a + " b: " + b);
}
}
{
public static void main(String[] args)
{ go();
}
public static void go()
{ int x = 3;
int y = 2;
System.out.println("In method go. x: " + x + " y: " + y);
falseSwap(x,y);
System.out.println("in method go. x: " + x + " y: " + y);
moreParameters(x,y);
System.out.println("in method go. x: " + x + " y: " + y);
}
public static void falseSwap(int x, int y)
{ System.out.println("in method falseSwap. x: " + x + " y: " + y);
int temp = x;
x = y;
y = temp;
System.out.println("in method falseSwap. x: " + x + " y: " + y);
}
public static void moreParameters(int a, int b)
{ System.out.println("in method moreParameters. a: " + a + " b: " + b);
a = a * b;
b = 12;
System.out.println("in method moreParameters. a: " + a + " b: " + b);
falseSwap(b,a);
System.out.println("in method moreParameters. a: " + a + " b: " + b);
}
}
String Manipulations :
A few brief examples of String manipulations.
public class StringExample
{
{
public static void main(String[] args)
{
{
String s1 = "Computer Science";
int x = 307;
String s2 = s1 + " " + x;
String s3 = s2.substring(10,17);
String s4 = "is fun";
String s5 = s2 + s4;
System.out.println("s1: " + s1);
System.out.println("s2: " + s2);
System.out.println("s3: " + s3);
System.out.println("s4: " + s4);
System.out.println("s5: " + s5);
//showing effect of precedence
x = 3;
int y = 5;
String s6 = x + y + "total";
String s7 = "total " + x + y;
String s8 = " " + x + y + "total";
System.out.println("s6: " + s6);
System.out.println("s7: " + s7);
System.out.println("s8: " + s8);
}
}
int x = 307;
String s2 = s1 + " " + x;
String s3 = s2.substring(10,17);
String s4 = "is fun";
String s5 = s2 + s4;
System.out.println("s1: " + s1);
System.out.println("s2: " + s2);
System.out.println("s3: " + s3);
System.out.println("s4: " + s4);
System.out.println("s5: " + s5);
//showing effect of precedence
x = 3;
int y = 5;
String s6 = x + y + "total";
String s7 = "total " + x + y;
String s8 = " " + x + y + "total";
System.out.println("s6: " + s6);
System.out.println("s7: " + s7);
System.out.println("s8: " + s8);
}
}
Array :
A sample Array examples.
public class ArrayExamples
{
public static void main(String[] args)
{
{
int[] list = {1, 2, 3, 4, 1, 2, 3};
findAndPrintPairs(list, 5);
bubblesort(list);
showList(list);
list = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
bubblesort(list);
showList(list);
list = new int[]{11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};
bubblesort(list);
showList(list);
list = new int[]{1};
bubblesort(list);
showList(list);
}
findAndPrintPairs(list, 5);
bubblesort(list);
showList(list);
list = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
bubblesort(list);
showList(list);
list = new int[]{11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};
bubblesort(list);
showList(list);
list = new int[]{1};
bubblesort(list);
showList(list);
}