Java
Introduction
Welcome to online web tutorials for java
Java[ http://java.sun.com ]is a free and open source high level programming language. It is simple as well as object oriented language. Till date, the Java platform has attracted more than 6.5 million software developers. Java Platform, Standard Edition (Java SE) lets you develop and deploy Java applications on desktops and servers, as well as today's demanding Embedded and Real-Time environments.From laptops to mobile phones, game consoles to scientific supercomputers, music players to the Internet,Set-top boxes to printers, Web cams to medical devices,Ooops that is a huge list to follow,Java is everywhere!!!!!
This set of tutorials will cover features and usage of Java version 1.6.x Please see the associated text box of individual spoken tutorials on the website to decide the versions of Java and OS to which it is applicable.
The Spoken Tutorial Effort for Java is contributed jointly by TalentSprint, Hyderabad and the Spoken Tutorial Team, IIT Bombay. Other contributors who helped in the creation of the scripts are ............
Basic Level
1. Installing the JDK & compiling HelloWorld program in Java
* Importance of java * Installing the JDK using Synaptic Package Manager on Ubuntu * Creating a source file in Text Editor * Creating a class HelloWorld * Saving the file with the same name as the class i.e. HelloWorld with .java extension * Compiling file using javac compiler, to create a .class file * Running the program
2. Accessing the variables and methods in Java
* Creating Student.java file in the Text Editor * Declaring the class Student * Datatypes used - int, float, String * Declaring the variables Name,RollNo,Percentage * Declaring and defining the methods getRollNo() and setRollNo() (The getter and setter methods). * Creating an instance of the class i.e. object using new operator. * Accessing variables and method using dot operator. * Saving the file * Compiling using the command javac Student.java in the terminal. * Running the program using command java Student and checking the output.
3. Use of Static
class static1 { static int b=10; public static int show(int a) { return a; } public static void main(String arg[]) { // static1 s=new static1(); System.out.println(show(b)); } }
4. The if Instruction Control Statements in Java
* if Statement * if-else Statement if(Boolean_expression)//Statement executes when the Boolean expression is true else //Statement executes when the Boolean expression is false Example - if(String password=="pratham"){System.out.print("login");} else { System.out.println("access denied"); } * Nested if's- one if statement inside another if statement
5. The switch Instruction control statement in Java
* switch(expression) case value : //Statements break; //optional //You can have any number of case statements. default : //Optional //Statements Example - int select=1; switch(select) { case 0: System.out.println("Number is zero"); break; case 1: System.out.println("Number is one"); break; default: System.out.println("Invalid Entry!"); }
6. The while and do-while Decision Control loops in Java
* while loop while(Boolean_expression true)// execute statements Example - int a=0; while(a<10) { System.out.println("Number is "+a); a++; } * do...while loop do { //Statements } while(Boolean_expression); Example - do { System.out.println("Number is "+a);a++; } while(a<10);
7. The for Decision Control loop in Java
*for(initialization; condition; increment/decrement)//execute statements Example - for(int a=0;a<10;a++) { System.out.println("Number is "+a); }
8. Providing constructors for the class
* Creating a class rectangle in the Text Editor. * Creating a constructor rectangle(int a,int b){x=a;y=b;} with two parameters. * Initializing the variables in the constructor * Declaring a method area(){return(x*y);} * Invoking the constructor and method. * Saving, compiling and executing the program and checking the output.
9. Single dimensional arrays in java
* Declaring an array - int num[]; * Defining an array Example - int num = new int[2]; * Initializing an array Example - int num[0]=45; * Anonymous arrays Example - int array1[] = {1, 2, 3, 4, 5};
10. Multi-dimensional arrays in Java
* Declaring an array - int TwoArray[ ][ ]; * Defining an array Example - int TwoArray[ ][ ] = new int [7 ][12 ]; * Initializing an array Example - int TwoArray[ 0][0 ] =72; * Ragged arrays Example - int[][] array = {{0,1,2,3,4}, {0,1,2}, {0}, {0,1,}};
11. Exception Handling in Java
* try{}catch(Exception e){error message} * Example- try { int c =8/0; } catch(Exception e) { System.out.print("Error: Division by zero"); }
12. Command Line Arguments
* Arguments given at run time. * Arguments are assigned to String array. * This String array is the parameter of the main function. public static void main(String arg[]) { System.out.println("hello "+arg[0]); }
13. Method parameters.
* Pass by reference * Pass by value
Intermediate Level
Advanced Level
Contributors and Content Editors
Arya Ratish, Minal, Nancyvarkey, PoojaMoolya, Pratham920, Pratik kamble, Priyacst