Java Learning--definition and operation of basic data types

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.