JAVA学习笔记(2)基本数据类型和基本操作

来源:互联网 发布:centos7安装内核源码 编辑:程序博客网 时间:2024/04/28 02:04

欢迎访问我的个人网站:http://www.qingshuimonk.com/


2.基本数据类型和基本操作

1.        字符串连接符:将两个字符串连接起来,如果一个不是字符串,则先将其值转换为字符串,再与另一个字符串相连。

PA:在源代码中,字符串常量不能跨行。

2.        数值直接量:在程序中直接出现的常量值。

3.        默认情况下整形直接量默认为十进制,八进制用0开头,十六进制用0X开头。

4.        浮点数默认为double型,double型比float型更精确。

5.        条件布尔运算符:&&,||,当判断一边的表达式即可确定整个表达式的值后,另外一边不进行计算。

无条件布尔运算符:&,|,两边都要进行计算。

尽量不适用无条件布尔运算符,降低程序执行效率且可能出错。

6.        从输入对话框获取输入,使用JOptionPane中的showInptutDialog方法,在运行时获得输入。

7.        showInputDialog的调用方法,Stringstring=JOptionPane.showInputDialog(null,x,y,JOptionPane.QUESTION_MESSAGE),第一个参数总是null,第二个参数是一个提醒用户的字符串,第三个参数表示输入对话框的标题,第四个参数可以是JOptionPane.QUESTION_MESSAGE,使对话框显示图标。

8.        字符串转换为int值,用Integer的parseInt方法:int intValue=Integer.parseInt(intstring);

转换为double用Double类的parseDouble方法。

9.        检测是否是闰年的代码:

import javax.swing.JOptionPane;public class IOstudy {public static void main(String args[]){//Prompt the user to enter a yearString yearString = JOptionPane.showInputDialog(null,"Enter a year","Input an integer",JOptionPane.QUESTION_MESSAGE);//Convert the string into an int valueint year = Integer.parseInt(yearString);//Check if the year is a leap yearboolean isLeapyear = ((year%4==0)&&(year%100!=0))||(year%400==0);//Display the resultif(isLeapyear){JOptionPane.showMessageDialog(null,year + "is a leap year.","result",JOptionPane.INFORMATION_MESSAGE);}else{JOptionPane.showMessageDialog(null,year + "is not a leap year","result",JOptionPane.INFORMATION_MESSAGE);}}}

11.        格式化输出:System.out.printf(format,items)

其中format是一个子串和格式描述符构成的字符串

Example:

         int count =5;

         double amount= 45.56;

         system.out.printf(“countis %d and amount is %f”,count,amout);

PS:%b,布尔型;%e,标准科学计数法形式

12.        默认情况下,浮点显示小数后六位,可以在描述符中指定宽度和精度

%x.yf,x为浮点数至少的位数(包含小数点),y小数的位数