Site icon TestingDocs.com

Sample Java Programs to Practice

Overview

We will practice basic programming in Java. We will discuss basic operations on data types like int and String. We will run the program and look at the desired output.

Problem Statement

Declare 2 datatype variables and follow the steps below to write your program in Java.

Steps:
1.Declare 2 variables: one int, one String variable for example  20 , John ( or the name of your best friend.)
2.Add value 10 to the above integer and print.
3.Concatenate Hi to your best friend’s name and print.

Sample:
20
John

 

Desired Output

Sample output of your program should be :
30
Hi John

Try to practice the code in your fav IDE like Eclipse or IntelliJ.

 

Sample Code Snippet

public class Day2 {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    
  int i = 20 ;
  String s = "John" ;
  
    
  System.out.println(i + 10);
  System.out.println("Hi " + s);
  
  System.out.println("Another way :");
  
  System.out.println(i + 10 );
  System.out.println("Hi ".concat(s));
  }

}

 

Output :

30
Hi John
Another way :
30
Hi John

The System class contains several useful class fields and methods. It cannot be instantiated, since its constructor System() is not visible and not public.

The following facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.

Conclusion

Explore the class System methods and their usage. Also, a handy way to explore things is to look at the Java API.

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version