8种数据类型之间的转换

博客围绕8种数据类型的转换展开,虽暂无具体内容,但核心聚焦于数据类型转换这一信息技术关键操作,在编程等领域有重要应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.itheima;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class 各种转换 {
	public static void main(String[] args) throws ParseException {
		/*
 * 1.基本数据类型转换
		 */
		//隐式转换 byte,short,char -- int -- long -- float -- double
		//强制转换 
		int a = 12;
		byte b = (byte) a;
		
		/*
 * 2.String StringBuilder
		 */
		//String to StringBuilder
		StringBuilder sb = new StringBuilder("abcde");
		//StringBuilder to String
		String s = sb.toString();
		
		/*
 * 3.String 和  数组
		 */
		   //String to 数组
		String ss = "abcdefg";
		char[] charArray = ss.toCharArray();
		byte[] bytes = ss.getBytes();
		   //数组 to String
		String bys = new String(bytes);
		String chs = new String(charArray);
		
 *4 String 和  基本数据类型
		//基本数据类型 to String
		int an = 10;
		String aa = an+"";
		String aa1 = String.valueOf(an);
		
		//String to  基本数据类型
		int bb = Integer.parseInt("123");
                      //String to int
		char charAt = "123".charAt(0);
                      //String to char
		
		/*
 *5 String 大小写转
		 */
		String bigSmall = "AbCdEf";
		String big = bigSmall.toUpperCase();
		String small = bigSmall.toLowerCase();
		
		/*
 *6 自动装箱和拆箱
		 */
		Integer i = 123;//自动装箱
		int ii = i;		//自动拆箱
		
		/*
 *7 Date 和 String

		Date d = new Date();
		SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = sdf.format(d);
                     //Date to String
		Date parse = sdf.parse(format);
                     //String to Date
	
* 8 Date 和 Calendar
		
		    Date date = new Date();
		Calendar cal = Calendar.getInstance();
		Date time = cal.getTime();
                   //Calendar to Date
		cal.setTime(date);
                   //Date to Calendar

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值