ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 基本数据类型

基本数据类型

原创 Linux操作系统 作者:sheisella 时间:2010-01-25 18:30:52 0 删除 编辑


基本数据类型

本身表示的是一个个具体的数值。其本身有取值的范围。

1.数值型

整数类型(bytes,Int)
小数类型(double..)

Int用的最多。
从长度上讲,Bytes(-128~127)

一个默认的数字类型就是Int类型。
数据类型间可以转换

 

class TestDemo1{
  public static void main(String arg[]){
   int y = 20;
   byte x =(byte)y;
 System.out.print(x + y);

}
};


byte x =(byte)y 就是强制将y由int转型为byte.
长度小的类型可以自动转换到长度大的类型,但是反之,则需要强制进行。


小数类型

一个默认的小数类型就是double类型。从DOUBLE到FLOAT就需要强制转型。

class TestDemo1{
  public static void main(String arg[]){
   double x = 33.333333333;
   float y = (float) x;
 System.out.print(x + y);

}
};


还有一种方法,在数字后面加f


 class TestDemo1{
   public static void main(String arg[]){
    float x = 33.333333333f;
    System.out.print(x);
 
 }
 };

长整型的数据中,也可以在尾巴加l。道理都一样的。

题目:

请计算如下结果:

 class TestDemo1{
   public static void main(String arg[]){
    System.out.print(1l + 11);
 }
 };

 

结果为12.


字符型

表示一个个的字符。字符一定要用单引号括起来。

 class TestDemo1{
   public static void main(String arg[]){
    char c = 'a';
  System.out.print(c);
 }
 };

字符和int也可以互相转换。


 class TestDemo1{
   public static void main(String arg[]){
    char c = 'a';
    int x = c;
  System.out.print(x);
 }
 };

a变为数字后 变成96,用的是UNICODE操作的。本身兼容ASCII码。

整数比字符范围要大,所以当整数换成字符的时候,要强制转换。

 class TestDemo1{
   public static void main(String arg[]){
    char c = 'a';
    int x = c;
    x ++;
    char y = (char)x;
  System.out.print(x);
 }
 };

结果为98.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22117706/viewspace-625974/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 基础
下一篇: 基本数据类型2
请登录后发表评论 登录
全部评论

注册时间:2009-11-15

  • 博文量
    27
  • 访问量
    16111