java output data type_input output and java data type

1. There are two ways to enter.

1. Use Scanner.

When using it, declare the import java.util.Scanner package at the beginning of the declaration. Then define the Scanner object:

Scanner  input=new Scanner(System.in);

Then declare the data type of your input, for example, the input is of type int:

int  intName=input.next();

2. Use BufferedReader.

When using, declare the reference import java.io.* package (including import java.io.BufferedReader and import java.io.InputStreamReader) at the beginning, and then define

BufferedReader input=new BufferedReader(new InputStreamReader(System.in));

Then declare the data type you want to receive, for example, if the tiger is of int type:

int intName=Integer.parseInt(input.readLine());

Note: Because the readLine() method declares an exception at the bottom! We have to deal with it when we use it.

Example

import java.io. *; // java.io 包括 import java.io.BufferedReader 和 import java.io.InputStreamReader;

public class HelloWorld {

public static void main(String[] args) throws IOException

{

System.out.println("Please enter a number: ");

BufferedReader input=new BufferedReader(new InputStreamReader(System.in));

int intName=Integer.parseInt(input.readLine());

System.out.println("The number you entered is: "+intName);

}

}

3.总结:当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并置入缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取。System.in是一个位流,为了转换为字符流,可使用InputStreamReader为其进行字符转换,然后再使用BufferedReader为其增加缓冲功能。所以使用BufferedReader input = new BufferedReader(new InputStreamReader(System.in))。

区别:(详情请见http://blog.sina.com.cn/s/blog_5fd837410100rtwk.html)

BufferedReader:是字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取!速度要比Scanner快!而且也可以设置缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。

Scanner:用Scanner获得用户的输入非常的方便,但是Scanner取得输入的依据是空格符,包括空格键,Tab键和Enter键。当按下这其中的任一键时,Scanner就会返回下一个输入.当你输入的内容中间包括空格时,显然,使用Scanner就不能完整的获得你输入的字符串。

二、输出的方式。

System.out.println。嗯。。。。这个确实没什么好说的。大家都知道。而且不用引用命名空间或者包。直接上例子:

1 public classHelloWorld {2 public static voidmain(String[] args){3 System.out.println("输出");4 }5 }

三、数据类型。

1.基本数据类型:

1).整型:byte(字节):byte由1个字节8位表示,是最小的整数类型。主要用于节省内存空间关键。当操作来自网络、文件或者其他IO的数据流时,byte类型特别有用。取值范围[-128,127]

short(短整型):由2个字节16位表示。short类型参与运算的时候,一样被提升为int或者更高的类型。取值范围[-2^15,2^15-1]

int(整型):由4个字节32位表示。取值范围[-2^31,2^31-1]

long(长整型):由8个字节64位表示。当需要计算非常大的数时,如果int不足以容纳大小,可以使用long类型。如果long也不够,可以使用BigInteger类。取值范围[-2^63,2^63-1]

2).浮点型:

float(浮点型):由4个字节32位表示。 单精度浮点数,运行速度相比double更快,占内存更小,但是当数值非常大或者非常小的时候会变得不精确。精度要求不高的时候可以使用float类型。可以将byte、short、int、long、char赋给float类型,java自动完成转换(隐式转换)。取值范围[-3.4E38,3.4E38]

double(双精度):由8个字节64位表示。将浮点数赋给某个变量时,如果不显示在字面值后面加f或者F,则默认为double类型。java.lang.Math中的函数都采用double类型。如果double和float都无法达到想要的精度,可以使用BigDecimal类。取值范围[-1.7E308,1.7E308]

3).字符型:char:占2个字节16个位。ASCII字符集占用了Unicode的前127个值。当char进行加减乘除运算的时候,也被转换成int类型,必须显式转化回来。取值范围[0,65535]

4).布尔型:boolean:只有两个值true和false,默认为false。boolean与是否为0没有任何关系,但是可以根据想要的逻辑进行转换。许多地方都需要用到boolean类型。

2.基本类型数据转换:

7dfe72fefcacff3bf3c487b92bbaaa2b.png

3.引用数据类型:类、接口类型、数组类型、枚举类型、注解类型。这里就不一一赘述。(关键我还没接触和熟悉。后面熟悉了会列举出来)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324063426&siteId=291194637