Example 1:
public class datademo05{
public static void Main (String args[]) {
char ch1 = ' \ ';//Represents a "
char CH2 = ' \ \ ';//Represents a \
System.out.println ("ch1 =" + ch1);
System.out.println ("CH2 =" + CH2);
System.out.println ("\" Hello world!\ "");
}
};
Operation Result:
CH1 = "
CH2 = \
"Hello world!"
Example 2: note: "F" after 3.0 is not less
public class datademo06{
public static void Main (String args[]) {
float num = 3.0f;//define a floating-point variable
System.out.println ("Two decimals multiplied:" + num * num);
}
};
Operation Result:
Multiply two decimals: 9.0
Example 3: Defining a Boolean variable
public class datademo07{
public static void Main (String args[]) {
Boolean flag = true;//define Boolean variable
SYSTEM.OUT.PRINTLN ("flag =" + flag);//Print output
}
};
Operation Result:
Flag = True
Example 4:
public class datademo08{
public static void Main (String args[]) {
int x = 30;//define Integer variable
Float y = 22.19f;//define floating-point variable
SYSTEM.OUT.PRINTLN ("x/y =" + (x/y));
System.out.println ("10/3.5 =" + (10/3.5));
System.out.println ("10/3 =" + (10/3));
}
};
Operation Result:
X/y = 1.3519603
10/3.5 = 2.857142857142857
10/3 = 3
Example 5
public class datademo09{
public static void Main (String args[]) {
String str = "Lixinghua";//define string variable
int x = 30;
str = str + x;//Modify the contents of STR and give the contents back to the STR variable
System.out.println ("str =" + str);
}
};
Operation Result:
str = Lixinghua30
Example 5
public class datademo10{
public static void Main (String args[]) {
int i = 1;//define integral type variable
int j = 2;//define integral type variable
System.out.println ("1 + 2 =" + 1 + 2);
System.out.println ("1 + 2 =" + (1 + 2));
}
};
Operation Result:
1+2=12
1+2=3
Example 6:
public class datademo11{
public static void Main (String args[]) {
float f = 30.3f;//floating-point type
int x = (int) f;//coercion type conversion
System.out.println ("x =" + x);
System.out.println ("10/3 =" + ((float) 10/3));//Perform the cast
}
};
Operation Result:
x = 30
10/3 = 3.3333333
Java Learning--definition and operation of basic data types