Scanner input = new Scanner(System.in);
System.out.print("Enter a double value: ");
Scanner input = new Scanner(System.in);
double d = input.nextDouble();
java.util.* ; // Implicit import
java.util.Scanner; // Explicit Import
//valid
_myvariable
_5variablename
$test_variable
TestVariable
//invalid
int
boolean
null
data type variable name = value;
int a, b, c; // Declares three ints, a, b, and c.
int a = 10, b = 10; // Example of initialization
byte B = 22; // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a'; // the char variable a iis initialized with value 'a'
// Compute the first area
double radius = 1.0;
double area = radius * radius * 3.14159;
System.out.println("The area is “ + area + " for radius "+radius);
int firstValue=10;
int secondValue=firstValue;
System.out.println(secondValue);
int x; // Declare x to be an integer variable;
double radius; // Declare radius to be a double variable;
char a; // Declare a to be a character variable;
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;
int x = 1;
double radius = 1.o;
char mychar='A';
Sample code on Github CheckDataType code
final datatype identifier_name = constant;
public class Main {
public static void main(String[] args) {
//what is the output of this code?
final int VALUE=123;
VALUE=567;
System.out.println(VALUE);
}
}
double area=0.0;
double radius;
double computeArea;
public class ComputeArea
final double PI=3.14d;
final float MAX_VALUE=10.2f;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print ("Enter the Qty: ");
int qty = input.nextInt();
System.out.println(qty);
}
}
How do you print your full name using Scanner class?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print ("Enter your full name: ");
String name = input.nextLine();
System.out.println(name);
}
}
+, -, *, /, and %
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
5 % 2 yields 1 (the remainder of the division)
3-2
System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);
displays 0.5000000000000001, not 0.5, and
System.out.println(1.0 - 0.9);
displays 0.09999999999999998, not 0.1.
System.out.println(Math.pow(2, 3)); // Displays 8.0
System.out.println(Math.pow(4, 0.5)); // Displays 2.0
System.out.println(Math.pow(2.5, 2));// Displays 6.25
System.out.println(Math.pow(2.5, -2)); // Displays 0.16
int i = 34;
long x = 1000000;
double d = 5.0;
byte b = 1000 ; //byte can hold -128 to 127 max
int j=2147483647+1;
System.out.println(j);
For example, you can use
float myFloatValue= 100.2f or 100.2F for a float number,
double myDoubleValue= 100.2d or 100.2D for a double number.
System.out.println("1.0 / 3.0 is " + 1.0 / 3.0); //16 digits
System.out.println("1.0F / 3.0F is " + 1.0F / 3.0F); //8 digits
double myDoubleValue= 100.2d or 100.2D for a double number.
for example, 1.23456e+2, same as 1.23456e2, is equivalent to 123.456
1.23456e-2 is equivalent to 0.0123456.
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is truncated)
What is wrong? int x = 5 / 2.0;
int sum = 0;
sum += 4.5; // sum becomes 4 after this statement
sum += 4.5 is equivalent to sum = (int)(sum + 4.5).
double interestRate = 0.05;
double interest = interestrate * 45;
int value = 2147483647 + 1;
// value will actually be -2147483648
System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);
System.out.println(1.0 - 0.9);
public class Main {
public static void main(String[] args) {
int value1=1;
int value2=2;
double average=(value1+value2)/2;
System.out.println(average);
}
}
public class Main {
public static void main(String[] args) {
int value1=1;
int value2=2;
double average=(value1+value2)/2.0;
System.out.println(average);
}
}
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int v1 = input.nextInt();
Scanner input1 = new Scanner(System.in);
System.out.print("Enter a double value: ");
double v2 = input1.nextDouble();