Tutorial
Creating objects and Calling Methods
Now that, we have coded a class, we will now use it to create different Student objects. We do all of this in a test class. ... ect: 98Marks in second subject: 100Marks in second subject: 96 Next : Get and Set MethodsPrev : The constituents of a Class
Searching and Sorting Arrays
An array is not just used to store elements but also access those stored values later on. We may also need to search for a p ... to learn about them, then refer to these pages on another site: Next : Array of objectsPrev : Processing arrays using loops
Throwing Exceptions
We have seen till now how exceptions thrown by methods we invoke such as nextInt() can be caught. We shall now see how we ou ... rintln(“Exception caught”); } }} The output of this program would be Exception caught Prev : Nested try catch blocks
Exception Handling
A program may not always reach to its end. It might be interrupted in several ways. A logical error in your program could cr ... have entered invalid dataEnter an integer: 347You entered 347 Next : Exception hierarchyPrev : Abstract classes and methods
Static Methods and Variables
Variables and methods of the Student class which we have written can be accessed only after creating an object of that class ... es from a static method. The Test class shows how static methods can be accessed using both the class name and method name.
Access Specifiers
We have quite often been using the access specifiers- public and private. We shall now see what these access specifiers actu ... owed not allowed
public allowed allowed allowed allowed
< unspecified > allowed not allowed allowed not allowed
Increment and Decrement Operators
Increment and decrement operators are widely used in controlling loops. These operators provide a more convent and compact ... -2 ) % 40 + 0 – ( -2 % 4 )( 0 + 0 ) – 2( 0 – 2 )-2 Next : Overloading constructors and methodsPrev : Final variables
Using Ellipsis to A!ccept Variable Number of Arguments
A method can receive a variable number of arguments with the use of ellipsis (three consecutive dots: …) In order to do so, ... ular method rather than the one taking variable number of arguments. Verify this fact yourself by modifying the code above.
Relation between a Super Class and Sub Class
A sub class has an ‘is a’ relationship with its superclass. This means that a sub class is a special kind of its super cl ... ble of type B. class A { B obj; } Class B { } Next : Final classes and methods Prev : Inheritance introduction
Get and Set methods
We were able to create objects of the Student and initialise the variables of the object using the constructor. But after th ... (String n, int rn, int m1, int m2, int m3) { name = n; setRollNumber(rn); setMarks1(m1); setMarks2(m2); setMarks3(m3); }
The constituents of a class
A class as already stated has three major components- variables, constructors and methods. A class may have all of these, ... : “+marks3); } } Next : Creating objects and calling methods Prev : Introduction to object oriented programming
Call by Value and Call by Reference
Before we look into what the terms call by value and call by reference mean, let us look at two simple programs and examine ... cond objectNumber c=b;// c also refers to the second objectboolean result1= a==b; // falseboolean result2= b==c; // true
Mathematical Operations in Java
We have already seen about mathematical calculations in Java. We will now recollect them and then look at a few other aspect ... yte varaibles. we cast num1 to double and perform the division in the following way. double result = (double) num1 / num2;
More about Data Types
Here, we will discuss more about the data types we have seen in the previous article.
Contents
Storing integers
Storing ... llowing shows an example. int a = 3; String str = “The value of a is ” + a; str now stores the String “The value of a is 3”
The Hello World Program
Java is an object oriented programming language. We write classes and build objects out of them. A class is a blueprint or a ... ever, here again “Hello World!” should be written on a single line. Strings in Java should begin and end on the same line.
Class as a Reference Data Type
It has already been said that a class is also a data type- a non primitive reference data type. This was very much implici ... . And then t1 is compared to t2. Run the program and see down the result. Next : Constants or literals Prev : Casting
Overloading Constructors and Methods
When more than a single constructor is defined in a class, it is known as constructor overloading. Similarly, when more th ... and would generate a compilation error. Next : Static methods and variables Prev : Increment and decrement operators
Final Classes and Methods
Inheritance is surely one of the highly useful features in Java. But at times, it may be desired that a class should not be ... as final. The example cited above is just illustrative. The actual scenario in real world programming is much more complex.
Variables and Data Types
Programs are often required to store information. For example, to write a program that adds two numbers, we need to store ... “+ firstNumber + ” and “+ secondNumber + ” is “+ result ); } } Next : More about data typesPrev : Hello World Program
The protected Access Specifier
As we have lardy said, there also exists the protected access specifier in addition to the public and private access specifi ... variables of class B are not accessible from class A. Similarly, protected variables of class A are not accessible from B.