---
title: "java 基础数据类型"
author: "ss1"
date: "2010-03-07T19:42:49Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2010-03-07T19:42:49Z"
id: 609747
url: "https://ss1.iteye.com/blog/609747"
---

1.简单数据类型  
逻辑类型  boolean  
文本类型  char,String(特殊类型)  
整数类型  byte,short,int,long  
浮点类型  double,float

boolean  两个值:true and false  
char  16位无符号(不分正负的)Unicode字符 必须包含在单引号内('')  
       eg:'\\t' 表示一个制表符  
            '\\u????' 表示一个特殊的Unicode字符 ????应严格按照4个16进制数进行替换.  
String  不是一个简单的数据类型 而是一个类(class) 它被用来表示字符序列.字符本身符合Unicode标准  
         且上述char类型的反斜线符号适用于String.  
         与c and c++ 不同,String不能用'\\o'作为结束. (本来要写'\\零'的,可是它就是不显示,没招儿了,就用o来代替吧~~)  
byte short int long  
         在数据后面直接跟一个字母"L".L表示这是一个long型数值.![](http://www.yculblog.com/images/emotions/3/17.gif) 在Java编程语言中L无论是大写还是小写都同样有效,但由于小写l与数字1容易混淆,因而,使用小写不是一个明智的选择.  
                                           整型数据的长度和数值范围  
   ----------------------------------------------------------------------------------------------------------------------  
        长度                                    数据类型                                  数值范围  
   ----------------------------------------------------------------------------------------------------------------------  
        8 bits                                     byte                                   -2(7)~2(7)-1  
        16 bits                                   short                                   -2(15)~2(15)-1  
        32 bits                                   int                                      -2(31)~2(31)-1  
        64 bits                                   long                                    -2(63)~2(63)-1  
   ---------------------------------------------------------------------------------------------------------------------  
float double     
          数值包含小数点或指数,或者在数字后面带有字母F or f(float), D or d(double)  
                                           浮点型数据的长度和数值范围  
   ----------------------------------------------------------------------------------------------------------------------  
        长度                                    数据类型                                  数值范围  
   ----------------------------------------------------------------------------------------------------------------------  
        32 bits                                   float                              1.401e-45~3.402e+38  
        64 bits                                   long                              4.94e-324~1.79e+308d  
   ----------------------------------------------------------------------------------------------------------------------

2.简单数据类型变量的声明和赋值  
public class Assign{  
  public static void main(String\[] args){  
     int x,y;                                          //declare int variables  
     float z=3.414f;                               //declare and assign float variable  
     double w=3.1415;                          //declare and assign double variable  
     boolean truth=true;                        //declare and assign boolean variable  
     char c;                                           //declare character variable  
     String str;                                       //declare String variable  
     String str1="bye";                            //declare and assign String variable  
  c='A';                                                //assign value to char variable  
  str="Hi out there!";                             //assign value to String variable  
  x=6;  
  y=1000;                                            //assign values to int variable  
  ...  
  }  
}

下面是一些非法赋值的语句:  
y=3.1415926;                               //is not an int.Requires casting and decimal will be truncated  
w=175,000;                                 //the comma symbol(,)cannot appear  
truth=1;                                      //a common mistake made by ex- c/c++ programmers  
z=3.14156;                                  //can't fit double into a Float