First Java program in
Eclipse:
package Package1;
public class FirstJavaProgram {
// we
have to write main()
public
static void main(String [] args)
{
// special method in java
///when we run java,
JVM or o/s Calls main- method by
default
// program execution always starts from main() method only
// Statement: collection of words + syntax rules
// Display a message "Hi Java" in the console
window/monitor
System.out.println("Hi
Java");
// Print the given message and it
goes to a new line
//println()
: predefined method in java
// Can be used to display the given message in the console
window/output window/monitor
//
Whatever we write in double quotes, it displays as it is..
// Semicolon ; is a statement terminator
// Every statement must end with a
semicolon;
// To display the message "How are you?"
System.out.println("How
are you");
//
print given msg and it goes to new line
}
}
run java :
right click inside main method > run as > Java
applictaion
o/p:
Hi Java
How are you
ex2:
package Package1;
public class JavaProgram2
{
// write main ()
-special method --
///when
we run java, JVM or o/s Calls main- method by default
//
Execution starts from main - method
public
static void main(String args[])
{
// Display msg in stmt-1
System.out.println("stmt-1");
//
// Display msg in stmt-2
System.out.println("stmt-2");
// Display msg in stmt-3
System.out.println("stmt-3");
//
Short cut :
// to write println () --
//type
syso + Press -(ctrl +
space) and press enter- it displays println() stmt fully
System.out.println("stmt-4");
System.out.println("stmt-5");
}
}
//o/p:
stmt-1
stmt-2
stmt-3
stmt-4
stmt-5
If we don’t have laptop, we can run java program in online
java -compiler
Refer:
https://www.programiz.com/java-programming/online-compiler/
println("Hi Java"); can be used to print the given msg and cursor goes to next line
print(): - is
predefined method in java
- can be used to
print the given msg in console window
and cursor is in the same line
print basics Program:
package Introduction;
public class printBasics {
public
static void main(String[] args)
{
System.out.print("Hi
Java"); // print given msgs
and cursor is in the same line
System.out.print("How
are you");
System.out.print("I
am fine");
}
}
/*
* o/p:
* Hi JavaHow are youI
am fine
*/
FAQ: Difference between println()
and print()
HW : write a code to display below o/p using
println and print()
o/p:
Hi Ram
Learn Java
sure
HW : display below o/p using println() and print()
o/p: Hi RamLearn Javasure
Write o/p for below
program?
package Introduction;
public class MixingPrintlnAndPrint
{
// write
main ()
public
static void main(String args[])
{
System.out.print("Hi
Java");
System.out.println("How
are you");
System.out.println("I
am fine");
System.out.print("What
about you.");
}
}
/* ctrl + /
or ctrl+ shft +/
* o/p:
* I am fine
* What about you.
*
*
*/
HW write o/p for below program:
System.out.println("Hi
Java");
System.out.print("How
are you");
System.out.print("I
am fine");
System.out.println("What
about you.");
\n - goes
to new line
ex:
package Package1;
public class MixingPrintlnAndPrint
{
public
static void main(String args[])
{
//\n - goes
to new line
System.out.print("Hi
Java\n");
System.out.print("How
are you\n");
System.out.print("I
am fine\n");
System.out.print("What
about you.\n");
}
}
o/p:
Hi Java
How are you
I am fine
What about you.
HW in the above
program replave \n by \t write the o/p?
\t -represents tab (Enter 5 spaces)
No comments:
Post a Comment