Introduction to Java Programming
class Demo{
public static void main(String[] param){
System.out.println("This text will be displayed");
}
}
Here, in the given program, name of class containing main() method is hello, so the name of file is hello.java. The case of class name and file name must match. The execution of the Java programs start from the main function(), main function takes parameter; main() method/function is defined as public and static. The public keyword is an access specifier; access specifier controls the visibility of class members: variables and methods; public members are accessible outside of the class in which it is declared. The main function is declared public as it must be called by code outside of its class at the time of program starting. The keyword static allows main() to be called without need to creat object of the class in which main() is defined. The keyword void is written as main() does not return any value.
Any information needed to pass to method is passed via parameters which are specified within a set of parenthesis of the method. In the main(), only one parameter is passed, and this is array of Strings.
System.out.println() method prints string passed to the println() function. In the above program, This text will be displayed in command line, is displayed when executed. All statements in Java end with a semicolon. Lines in the program not ending with a semicolon are not technically statements in Java.
Data Types: Data types specify type of values that to store. To store numbers int, long, byte, short are used; to store real numbers float or double is used, to store character, char data type is used. For integer variables, range of values are specified, each type of integer has range of values it can store. All integers in Java are signed, positive and negative values; unsigned, positive only integers are not supported by Java.
Name | Width in Bits | Description | Range |
---|---|---|---|
double | 64 | double specifies double precision value | |
float | 32 | single precision value | |
char | 16 | Java uses Unicode to represent characters | |
boolean | Used for logical values, it has only two values: true or false. | ||
int | 32 | Any whole number value is an integer literal. | -2,147,483,648 to 2,147,483,647 |
long | 64 | Any whole number value is an integer literal. Used to store big numbers | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
short | 16 | Any whole number value is an integer literal. | -32,768 to 32,767 |
byte | 32 | Used to store small integers. | -128 to 127 |
Declaring a Variable
type indentifier [=value][,indentifier[=value]...];
int a, b, c;
int x = 44, y = 45;
double pi = 3.14159;
char a = 'x';
class hypotenus{
public static void main(String[] param){
double b = 3.0, p = 5.0;
double hypo = Math.sqrt(b*b + p*p);
System.out.println("Hypotenuse is: "+hypo);
}
}
Here in the class, hypotenus, b and p are initialized with constants but hypo is initialized dynamically. This program uses Java's built-in method, sqrt(), member of Math class, to compute the square root of the argument (parameter) passed to it.
Scope and Lifetime of Variables
Variables can be declared within any block. A block begins with an opening curly brace and ends with closing curly brace. A block defines a scope. A scope determines what objects or variables are visible to other parts of your program and also determines the lifetime of those objects or variables.
In Java, two major scopes are defined; one by a class and another by a method. Generally, variables declared in one scope are not visible to code that is defined outside that scope. Scopes can be nested.
class JavaDemo
{
public static void main(String[] oops){
int x;
x = 10;
{
int y = 35;
System.out.println("Sum of "+x+" and "+y+" is: "+(x+y));
x = x + 100;
}
// y = 33; not possible to access y as it is in another scope
System.out.println("Value of x is: "+x); //displays 110
}
}
String Handling in Java
Introduction to C Programming
Woah
ReplyDelete