Documente Academic
Documente Profesional
Documente Cultură
Automatic Conversion
In Java type conversions are performed automatically when the type
of the expression on the right hand side of an assignment operation
can be safely promoted to the type of the variable on the left hand
side of the assignment. Thus we can safely assign: byte > short >
int > long > float > double. The > symbol used here should be
interpreted as "to a". For example:
// 64 bit long integer
long myLongInteger;
// 32 bit standard integer
int myInteger;
myLongInteger = myInteger;
The extra storage associated with the long integer, in the above
example, will simply be padded with extra zeros.
Here, type specifies the type of data returned by the method. This can
be any valid type including class types that you create. If the method does
not return a value, its return type must be void. The name of the method is
specified by name. This can be any legal identifier. The parameter list is a
sequence of type and identifier pairs separated by commas.
Methods that have a return type other than void return a value to the
calling routine using the following form of the return statement:
return value;
11). We declare, main method in java as: public static void main(String
args[])
static : main is the entry point of a class. In java everything thing is written
in a class.Now when you run java filename on command prompt, loader
will load the class and jvm will search the main method to enter into the
class. so making the main() as static, will make jvm access it directly
through classname.main()
Now once jvm get the main(), object instantiated there are accessed.
main : its the method name, the JVM will seek for after the class is loaded
on runing command like e.g java classname
String args[] : String array named args used to access the string variables
passed on command line. e.g java classname firstname lastname
public ;
The class Account defines the show () method and various data
members, such as name and account number. All the classes in the
program can access the various details of a customer, such as name
and account number. Therefore, these data members and the method
are declared public. The show () method is used to display the
account number and customer name of a customer.
You can use the following code snippet to define a class, Account that
contains public data variables and method:
protected ;
In an airline reservation application, you can create the
Ticket class that consists of various data members, such as
flightNumber, date, time, and destination. You can derive the
ConfirmedTicket subclass from the Ticket class that consists
of an additional data member, seatNumber. You can declare
the data members of the Ticket class as protected, which
can be accessed by the ConfirmedTicfcet subclass. You can
use the following code snippet to define the Ticket class that
has protected data variables:
Class Books
{
Int pageNumbers;
float price;
void showdata()
{
//code body
}
}
In Java, friendly is not a keyword. It is a term that is
used for the access level when no access specifier has been
specified. You cannot declare a class, variable, or method
with the friendly specifier.
The current class instance. Can be used with variables (line 6) or methods (line 10).
1 class Numbers {
2 private int aNumber = 42;
3
4 public int returnANumber()
5 {
6 return this.aNumber;
7 }
8 public int returnANumber(int intIn)
9 {
10 return (intIn * this.returnANumber());
11 }
12
13 public static void main(String[] args) {
14
15 Numbers numberTest = new Numbers();
16
17 System.out.println("The Number is " +
numberTest.returnANumber() );
18 //output is: The Number is 42
19 System.out.println("The Number is " +
numberTest.returnANumber(2) );
20 //output is: The Number is 84
21 }
22 }
super
import java.io.*;
class Primenumberexample
{
public static void main(String[] args) throws Exception
{
BufferedReader bf = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter Limit to Print Prime Number :");
int k = Integer.parseInt(bf.readLine());
System.out.println("Prime Numbers Are : ");
for (int i=1; i < k; i++ )
{
for (int j=2; j < i; j++)
{
int a = i%j;
if (a==0){
break;
}
}
if(i == j)
{
System.out.print(" "+i);
}
}
}
}