1. System.out.println();
2. System.out.print();
3. Empty line
4. System.err.print();
5. Print text inside double quotes " "
6. Print file/folder path
7. Tab \t
8. New line \n
9. Break long statements/Text/Sentences and print
10. Print Variables
Let's start.
Open workspace in eclipse.
And create a new java project , a package , a class and main method.
1. Let's see 1st statement from above list.
System.out.println(); -- > Let's print Hello World!
System.out.println("Hello Wrold!");
System.out.println("Welcome.");
Answer:
Hello Wrold!
Welcome.
2. Let's see 2nd statement from above list.
System.out.print();
System.out.print("Hello Wrold!");
System.out.print("Welcome.");
Answer:
Hello Wrold!Welcome.
3. Let's see 3rd statement from above list.
Print Empty line:
System.out.println("Hello Wrold!");
System.out.println();
System.out.println("Welcome.");
Answer:
Hello Wrold!
Welcome.
4. Let's see 4th statement from above list.
System.err.print(); --> This prints error message in RED color.
System.err.println("This is error message.");
Answer:
This is error message. AND this text is in RED color.
5. Let's see 5th statement from above list.
Print text inside double quotes " "
System.out.println("He said, \"Good Bye.\" and left.");
Answer:
He said, "Good Bye." and left.
6. Let's see 6th statement from above list.
Print file/folder path
System.out.println("The file path is C:\\MainFolder\\SubFolder\\Test.txt");
OR
This can be done in a different way also, let's see:
System.out.println("The file path is C:/MainFolder/SubFolder/Test.txt");
Answer:
The file path is C:\MainFolder\SubFolder\Test.txt
OR
The file path is C:/MainFolder/SubFolder/Test.txt
7. Let's see 7th statement from above list.
Tab \t --> This inserts tab space in the statements at desired point.
System.out.println("The title is\tFREEDOM");
System.out.println("The title is\t\tFREEDOM");
Answer:
The title is FREEDOM
The title is FREEDOM
8. Let's see 8th statement from above list.
New line \n --> This breaks the line at desired point and moves the remaining portion after \n to the new line.
System.out.println("The title is\n\nFREEDOM");
Answer:
The title is
FREEDOM
9. Let's see 9th statement from above list.
Break long statements/Text/Sentences and print
System.out.println("This world is beautiful because of rivers,"
+ " hills, mountains, animals,"
+ " and good people.");
Answer:
This world is beautiful because of rivers, hills, mountains, animals, and good people.
10. Let's see 10th statement from above list.
Print Variables --> For this tutorial, let's see with two variables , int and String.
int i = 10; // int variable.
System.out.println(i);
System.out.println("The value of i is : "+i);
String name = "Paris"; // String variable.
System.out.println(name);
System.out.println("The number of tourists from "+name+" are "+i);
Answer:
10
The value of i is : 10
Paris
The number of tourists from Paris are 10
Tab \t --> This inserts tab space in the statements at desired point.
System.out.println("The title is\tFREEDOM");
System.out.println("The title is\t\tFREEDOM");
Answer:
The title is FREEDOM
The title is FREEDOM
8. Let's see 8th statement from above list.
New line \n --> This breaks the line at desired point and moves the remaining portion after \n to the new line.
System.out.println("The title is\n\nFREEDOM");
Answer:
The title is
FREEDOM
9. Let's see 9th statement from above list.
Break long statements/Text/Sentences and print
System.out.println("This world is beautiful because of rivers,"
+ " hills, mountains, animals,"
+ " and good people.");
Answer:
This world is beautiful because of rivers, hills, mountains, animals, and good people.
10. Let's see 10th statement from above list.
Print Variables --> For this tutorial, let's see with two variables , int and String.
int i = 10; // int variable.
System.out.println(i);
System.out.println("The value of i is : "+i);
String name = "Paris"; // String variable.
System.out.println(name);
System.out.println("The number of tourists from "+name+" are "+i);
Answer:
10
The value of i is : 10
Paris
The number of tourists from Paris are 10
No comments:
Post a Comment