Beginners Programs :

Java : Print Hello Gretting Before Name | Java Programs

Hello Java program – now you can showoutput as Hello, before any of your name. The following code is written in different ways, check out.

This code is used to print out or display a greeting by taking input from a user using Java language.

  • The problem here is to print out a greeting addressing someone (example : Hello Gerald) by taking a name as an input from the user.
  • The input here is a string value input by the user. This user input string is addressed by a greeting in the output.
  • The output here is a greeting followed by the user input string.
 

Output:

  • Solution :
  1. The class Greeting comprises of the main method which encompasses the code for printing out the greeting.
  2. The first line in the main method initializes a char variable having value “. We shall understand the reason for this in the upcoming steps.
  3. In the main method, a new Scanner class object is initialized and a reference variable sc is set to represent the object.
  4. This class is used to take user input in Java. Scanner class is part of the java.util package and hence the import statement in the beginning is stated to import the functionality of the specified class.
  5. The next line prints a statement to the console instructing the user to enter a string.
  6. The .nextLine() method is called by the Scanner object sc. This method reads a line of user input of a string value and stores it to the string variable name.
  7. In the last line, the greeting is printed out along with the word input by the user. The output consists of the ” symbol (in “Hello Tiffany!”). However, the ” symbol is used to define the statement to be printed in the System.out.print method. This the reason as to why the char variable c representing the ” symbol is used to print the apostrophe.
    Note : The + symbol in the print statement is used for combining strings. This process of combining strings is called string concatenation.
 

Java Print Hello Program

This code is used to print out or display a greeting by taking input from a user using Java language.

  • The problem here is to print out a greeting addressing someone (example : Hello Gerald) by taking a name as an input from the user.
  • The input here is a string value input by the user. This user input string is addressed by a greeting in the output.
  • The output here is a greeting followed by the user input string.
 

Output:

  • Solution :
  1. The class Greeting comprises of the main method which encompasses the code for printing out the greeting.
  2. The first line in the main method initializes a char variable having value “. We shall understand the reason for this in the upcoming steps.
  3. In the main method, a new Scanner class object is initialized and a reference variable sc is set to represent the object.
  4. This class is used to take user input in Java. Scanner class is part of the java.util package and hence the import statement in the beginning is stated to import the functionality of the specified class.
  5. The next line prints a statement to the console instructing the user to enter a string.
  6. The .nextLine() method is called by the Scanner object sc. This method reads a line of user input of a string value and stores it to the string variable name.
  7. Within the print statement in the last line, the method helloName(name)  is called and the variable name is passed as an argument. helloName is a static method which means that this method does not belong to any particular instance of the class or an object but it belongs to the class itself. It can be called by the class directly and does not need an object to call it.
  8. The method concatenates or combines the greeting Hello to the user input word and returns the concatenated string which gets printed out to the console by the print statement in the final line of the main method.
  9. The char c is used to print the symbol ” as also done in Type 1. Read step vi. of type 1 for a better explanation on the usage of the same.