码迷,mamicode.com
首页 > 编程语言 > 详细

java学习笔记之一------数据类型及常用转换

时间:2015-04-27 00:41:31      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:java对象类型 类型转换

java数据类型划分

技术分享

分为两大类型:

               1)基本数据类型:类似于普通的值。

               2)引用数据类型:传递的是内存的地址。

浮点类型实际上就是表示小数。

 

java基本数据类型

技术分享

 

数据的溢出

当整数的数据大小超出了可以表示的范围,而程序中又没有做数值范围的检查时,这个整型变量所输出的值将发生絮乱,且不是预期的运行结果。

例如:求出整型的最大值

[java] view plaincopy

  1. public class T {  

  2.     public static void main(String[] args) {  

  3.         int max = Integer.MAX_VALUE;  

  4.         System.out.println("整型的最大值为:"+max); //整型的最大值为:2147483647  

  5.     }  

  6. }  


现在对求的最大值进行加法操作

[java] view plaincopy

  1. public class T {  

  2.     public static void main(String[] args) {  

  3.         int max = Integer.MAX_VALUE;  

  4.         System.out.println("整型的最大值为:"+max);       //整型的最大值为:2147483647  

  5.         System.out.println("整型的最大值+1: "+(max+1));  //整型的最大值+1: -2147483648  

  6.         System.out.println("整型的最大值+2: "+(max+2));  //整型的最大值+2: -2147483647  

  7.     }  

  8. }  


如果现在要想避免数据的溢出,可以采用扩大数据类型的方式。int-->long

[java] view plaincopy

  1. public class T {  

  2.     public static void main(String[] args) {  

  3.         int max = Integer.MAX_VALUE;  

  4.         System.out.println("整型的最大值为:"+max);       //整型的最大值为:2147483647  

  5.         System.out.println("整型的最大值+1: "+(max+1));  //整型的最大值+1: -2147483648  

  6.         System.out.println("整型的最大值+2: "+(max+2));  //整型的最大值+2: -2147483647  

  7.         System.out.println("整型的最大值+2: "+((long)max+2));  //2147483649  

  8.     }  

  9. }  


字符类型

    字符类型在内存中占有2个字节,可以用来保存英文字母等字符。计算机处理字符类型时,是把这些字符当成不同的整数来看待,

    因此,严格说来,字符类型也算是整数类型的一种。

[java] view plaincopy

  1. public class T {  

  2.     public static void main(String[] args) {  

  3.         char ch1 = ‘a‘;     //字符是使用‘‘括起来的数据  

  4.         char ch2 = 97;      //通过数字定义字符变量  

  5.         System.out.println("ch1 = "+ch1);  

  6.         System.out.println("ch2 = "+ch2);  

  7.     }  

  8. }  


常用的转义字符

技术分享

 

浮点数类型与双精度浮点数类型

    在日常生活中经常会使用到小数类型的数值,如身高,体重等需要精确的数值时,整数就不能满足程序设计者的要求了。在数学中,这些带有小数点的数值

   称为实数,在java中,这种数据类型称为浮点数类型(floag),其长度为32个字节,有效范围为-3.4E1038到3.4E1038。当浮点数的表示范围不够大的时候

   还有一种双精度(double)浮点数可供使用。双精度浮点数类型的长度为64个字节,有效范围为-1.7E10308到1.7E10308

 

在java 中一个数字或者一个小数实际上也都是存在默认类型的:

               小数(1.1,1.2)的默认类型是double类型

               整数(1,2,3)的默认类型是int类型

 

布尔类型

     布尔(boolean)类型的变量,只有 true(真)和false(假)两种

 

基本数据类型的默认值

技术分享


将int转换成String类型,方法有以下三种:

(1) String s = String.valueOf(i);

(2) String s = Integer.toString(i);

(3) String s = "" + i;


String -> int

s="12345";
int i;
第一种方法:i=Integer.parseInt(s);
第二种方法:i=Integer.valueOf(s).intValue();
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?

以下是答案:

第一种方法:s=i+"";   //会产生两个String对象
第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象

第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常
第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象

--------------------------------------------------------------------

1如何将字串 String 转换成整数 int?

A. 有两个方法:

1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

注: 字串转成 Double, Float, Long 的方法大同小异.

2 如何将整数 int 转换成字串 String ?

A. 有叁种方法:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i);

3.) String s = "" + i;

注: Double, Float, Long 转成字串的方法大同小异.



JAVA数据类型转换 

这是一个例子,说的是JAVA中数据数型的转换.供大家学习

package shenmixiaozhu;
import java.sql.Date;
public class TypeChange {
   public TypeChange() {
   }
   //change the string type to the int type
   public static   int stringToInt(String intstr)
   {
     Integer integer;
     integer = Integer.valueOf(intstr);
     return integer.intValue();
   }
   //change int type to the string type
   public static String intToString(int value)
   {
     Integer integer = new Integer(value);
     return integer.toString();
   }
   //change the string type to the float type
   public static   float stringToFloat(String floatstr)
   {
     Float floatee;
     floatee = Float.valueOf(floatstr);
     return floatee.floatValue();
   }
   //change the float type to the string type
   public static String floatToString(float value)
   {
     Float floatee = new Float(value);
     return floatee.toString();
   }
   //change the string type to the sqlDate type
   public static java.sql.Date stringToDate(String dateStr)
   {
     return   java.sql.Date.valueOf(dateStr);
   }
   //change the sqlDate type to the string type
   public static String dateToString(java.sql.Date datee)
   {
     return datee.toString();
   }

   public static void main(String[] args)
   {
     java.sql.Date day ;
     day = TypeChange.stringToDate("2003-11-3");
     String strday = TypeChange.dateToString(day);
     System.out.println(strday);
   }

}


有一个小程序,需要把byte数组转换成string,然后写文件,再读文件,再把string转换回来,成为byte数组。可是,看上去很简单,编出程序来一编译,通过..其实有玄机...特别是你要用到它(byte[]的内容)加密,解密时,就会报异常。至少我的会。出的异常是: javax.crypto.BadPaddingException:   Given   final   block   not   properly   padded   
上网找了很久,说法很多。自己搞了很久...终于搞定。

具体做法:

将byte数组转换成string
  
  BASE64Encoder enc=new BASE64Encoder();
  String 转换后的string=enc.encode(byte数组);
  
将string转换回来成为byte数组:  

  BASE64Decoder dec=new BASE64Decoder(); 
  try {
   byte数组 = dec.decodeBuffer(转换后的string);
  } catch (IOException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }


分享几个链接:

http://gagaghost.itpub.net/post/25/283173

http://topic.csdn.net/t/20061123/11/5179125.html

http://blog.csdn.net/dynamo2/archive/2005/08/30/468105.aspx

http://bbs.chinajavaworld.com/thread.jspa?threadID=677035&messageID=677189


java学习笔记之一------数据类型及常用转换

标签:java对象类型 类型转换

原文地址:http://7635866.blog.51cto.com/7625866/1638769

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!