0% found this document useful (0 votes)
4 views4 pages

Java Input and Default Values Guide

Uploaded by

Rameshkumar M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Java Input and Default Values Guide

Uploaded by

Rameshkumar M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Ex.

No: 1
Input Output Example

Aim:

To write a Java program that demonstrates how to accept user input using the Scanner class and print the
output using the [Link] statement.

Algorithm:
 Import the Scanner class and initialize a Scanner object to read user input.
 Display the message: "Enter your name: “
 Use [Link]() to read the user's name and store it in a variable name.
 Display the message: "Enter your age: “
 Use [Link]() to read the user's age and store it in a variable age.
 Print: "Hello, [name]!" and "You are [age] years old."
 Close the Scanner object and end the program.
Program
import [Link];

public class InputOutputExample {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link](); //
[Link]("Enter your age: ");
int age = [Link](); // Read the
[Link]("Hello, " + name + "!");
[Link]("You are " + age + " years old.");
[Link]();
}
}
Output
Enter your name: Alice
Enter your age: 25
Hello, Alice! You are 25 years old.

Result
The program successfully accepts the user's name and age as input and displays a greeting message
along with the entered age.

[Link]: 2 Display Default Values of Primitive Data Types in Java


Aim:

To display the default values assigned to each of the primitive data types in Java.

Algorithm:

 Declare variables for each primitive data type (byte, short, int, long, float, double, char,
boolean).
 Print the default value of each variable using [Link]().
 Use default values assigned automatically by Java (e.g., 0 for numeric types, false for boolean, '\
u0000' for char).
 Ensure proper formatting when printing each value (e.g., avoid extra spaces or characters).
 Display the results on the console.

Program:
class DefaultValues {
public static void main(String[] args) {
byte byteValue = 0;
short shortValue = 0;
int intValue = 0;
long longValue = 0L;
float floatValue = 0.0f;
double doubleValue = 0.0d;
char charValue = '\u0000';
boolean booleanValue = false;
[Link]("Default value of byte: " + byteValue);
[Link]("Default value of short: " + shortValue);
[Link]("Default value of int: " + intValue);
[Link]("Default value of long: " + longValue);
[Link]("Default value of float: " + floatValue);
[Link]("Default value of double: " + doubleValue);
[Link]("Default value of char: '" + charValue + "'");
[Link]("Default value of boolean: " + booleanValue);
}
}
Output:

Default value of byte: 0


Default value of short: 0
Default value of int: 0
Default value of long: 0
Default value of float: 0.0
Default value of double: 0.0
Default value of char: ' '
Default value of boolean: false

Result:
Thus the program prints the default value of all primitive datatypes successfully.

You might also like