Accepting The Values Dynamically
In java programming the values can be accepted dynamically through the java programming in four ways.
1. Through command prompt
2. Through keyboard
3. Through properties\resource bundle files
4. Through Xml document.
Length: -
length is one of the implicit variable created by JVM and supplied to each and every java program for two purposes they are
1. To find the number of elements present in the arrays (or) size of the array.
2. To treat fundamental arrays as Objects.
Syntax: -
Arrayname.length
Ex: -
Int a[] = { 10, 20, 30}
System.out.println(“no of elements in a =”+a.length);
for(int i=0; i<a.length; i++)
{
System.out.println(a[i]);
}
Accepting the data dynamically from command prompt: -
The values sending to the java program from the command prompt are known as commandline arguments
Consider the following statements
D:\JavaExamples> javac SunDemo 100 200
| | |
Command prompt Java program Values
In java programming what are all the type of command line arguments we are passing from command prompt, they are available in the main() in the form of array of objects of String Class.
Public static void main(String args[ ])
1. Through command prompt
2. Through keyboard
3. Through properties\resource bundle files
4. Through Xml document.
Length: -
length is one of the implicit variable created by JVM and supplied to each and every java program for two purposes they are
1. To find the number of elements present in the arrays (or) size of the array.
2. To treat fundamental arrays as Objects.
Syntax: -
Arrayname.length
Ex: -
Int a[] = { 10, 20, 30}
System.out.println(“no of elements in a =”+a.length);
for(int i=0; i<a.length; i++)
{
System.out.println(a[i]);
}
Accepting the data dynamically from command prompt: -
The values sending to the java program from the command prompt are known as commandline arguments
Consider the following statements
D:\JavaExamples> javac SunDemo 100 200
| | |
Command prompt Java program Values
In java programming what are all the type of command line arguments we are passing from command prompt, they are available in the main() in the form of array of objects of String Class.
Public static void main(String args[ ])
Note: -
Sun Microsystem has with draw the decision of writing/using eight main(-) for taking eight fundamental arrays(int array, float array etc…) because it is a complex for the java programmer to write each and every time eight main (-) for providing more simplicity and to make every command line argument as an Object, they have chosen they single main(-) by taking array of object of String class.
Converting String data in to Fundamental/numerical data: -
We know that whatever the command line arguments we are entering from command prompt to the java program, they are sending to the main(-) and available in the form of array of objects of String class.
We know that on String data we cannot perform any numerical manipulations. So that it is highly desirable to convert numerical String data in to Numerical/fundamental values.
The following table gives Fundamental Data types, Corresponding Wrapper class names and conversion method from Numerical String in to Numerical Value.
Sun Microsystem has with draw the decision of writing/using eight main(-) for taking eight fundamental arrays(int array, float array etc…) because it is a complex for the java programmer to write each and every time eight main (-) for providing more simplicity and to make every command line argument as an Object, they have chosen they single main(-) by taking array of object of String class.
Converting String data in to Fundamental/numerical data: -
We know that whatever the command line arguments we are entering from command prompt to the java program, they are sending to the main(-) and available in the form of array of objects of String class.
We know that on String data we cannot perform any numerical manipulations. So that it is highly desirable to convert numerical String data in to Numerical/fundamental values.
The following table gives Fundamental Data types, Corresponding Wrapper class names and conversion method from Numerical String in to Numerical Value.
In general each and every wrapper class is containing the following generalizing method for converting Numerical String values in to numerical Values.
Wrapper class name
Public static xxxx parseXxxx(String)
Here xxx --> represents any fundamental data type but not char.
Ex: -
String s[] = {10, 20}
System.out.println(s[0],s[1]); // 1020 concatenation
Int x= Integer.parseInt(s[0]);
Int x= Integer.parseInt(s[0]);
Int z = x+y;
System.out.println(“Sum =” +z); // 30
System.out.println(“Sum =” + x+y); // 1020
System.out.println(“Sum =”+ (x+y)); // 30
Wrapper class name
Public static xxxx parseXxxx(String)
Here xxx --> represents any fundamental data type but not char.
Ex: -
String s[] = {10, 20}
System.out.println(s[0],s[1]); // 1020 concatenation
Int x= Integer.parseInt(s[0]);
Int x= Integer.parseInt(s[0]);
Int z = x+y;
System.out.println(“Sum =” +z); // 30
System.out.println(“Sum =” + x+y); // 1020
System.out.println(“Sum =”+ (x+y)); // 30
Define a Wrapper class? What is it purpose?
For each and every data type there is a pre-defined class. Such pre-defined class are known as Wrapper class the purpose of wrapper class is that converting Numerical String data in to Numerical Data.
Write a java program which will inter changed two values (or) Swap two values?
//SwapDemo.java
class Swap
{
int a,b,t;
void assign(int x, int y) //2. next control comes here and the pass the values
here and store the values in to a and b and goes
back to main()
{
a=x;
b=y;
}
void process() // 3. next control comes here and swap the values and
goes back to main()
{
t=a;
a=b;
b=t;
}
void display() //1. first control comes here display the default values and
goes back to main()
//4. next control comes here and display the original values
and goes back to main().
{
System.out.println("The value of a ="+a);
System.out.println("The value of b ="+b);
}
};
class SwapDemo
{
public static void main(String[] args) // JVM search for main() and control comes
here and hold the command prompt values
{
System.out.println("I am from main(-) ----> begin");
if(args.length!=2)
{
System.out.println("Please enter two values");
}
else
{
System.out.println("the size of the array is " +args.length);
int x= Integer.parseInt(args[0]); // converting String data into
integer data
int y= Integer.parseInt(args[1]); // converting String data into
integer data
Swap s = new Swap(); // object creation
s.display();
s.assign(x,y);//2. control come back and control goes to Swap class
search for assign method and pass the values to
that method (x,y)
s.process();// control come back and control goes to Swap class
search for process method and executes it
s.display();// control come backand control goes to Swap class
search for display method and executes it
System.out.println("sum is"+" " +(x+y)); // print sum of 2 numbers
which we given in cmd prompt values
}
System.out.println("I am from main(-) ----> end"); // finally control
}
}
For each and every data type there is a pre-defined class. Such pre-defined class are known as Wrapper class the purpose of wrapper class is that converting Numerical String data in to Numerical Data.
Write a java program which will inter changed two values (or) Swap two values?
//SwapDemo.java
class Swap
{
int a,b,t;
void assign(int x, int y) //2. next control comes here and the pass the values
here and store the values in to a and b and goes
back to main()
{
a=x;
b=y;
}
void process() // 3. next control comes here and swap the values and
goes back to main()
{
t=a;
a=b;
b=t;
}
void display() //1. first control comes here display the default values and
goes back to main()
//4. next control comes here and display the original values
and goes back to main().
{
System.out.println("The value of a ="+a);
System.out.println("The value of b ="+b);
}
};
class SwapDemo
{
public static void main(String[] args) // JVM search for main() and control comes
here and hold the command prompt values
{
System.out.println("I am from main(-) ----> begin");
if(args.length!=2)
{
System.out.println("Please enter two values");
}
else
{
System.out.println("the size of the array is " +args.length);
int x= Integer.parseInt(args[0]); // converting String data into
integer data
int y= Integer.parseInt(args[1]); // converting String data into
integer data
Swap s = new Swap(); // object creation
s.display();
s.assign(x,y);//2. control come back and control goes to Swap class
search for assign method and pass the values to
that method (x,y)
s.process();// control come back and control goes to Swap class
search for process method and executes it
s.display();// control come backand control goes to Swap class
search for display method and executes it
System.out.println("sum is"+" " +(x+y)); // print sum of 2 numbers
which we given in cmd prompt values
}
System.out.println("I am from main(-) ----> end"); // finally control
}
}
Visitors