龙空技术网

朝文分享(2):C与Java之间的异同(一)

LearningYard学苑 193

前言:

而今同学们对“js javatype”大约比较注意,大家都需要知道一些“js javatype”的相关文章。那么小编同时在网摘上收集了一些有关“js javatype””的相关内容,希望小伙伴们能喜欢,大家一起来了解一下吧!

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是LearningYard新学苑。

今天小编为大家带来

“ C与Java之间的异同(一)”

欢迎您的访问。

Share interest, spread happiness, increase knowledge, and leave beautiful.

Dear, this is the LearingYard Academy!

Today, the editor brings the

Similarities and Differences between C and Java (1),

Welcome to visit!

一、定义变量的异同

在C中, 变量的定义较简单, 但是字符串需要用 “char”型数组来定义.

In C, the definition of variables is simpler, but strings need to be defined with an array of type "char".

在Java中, 基本类型变量的定义与C 是差不多的. Java不仅包含了C中的而且有了扩充, 如范围为 -128~127的 “byte”类型, 表示真假的 “boolean”类型,不用数组定义的 “String”类型. 此外, 在Java中 “float”和“long”的定义需要在后面加个代表类型的字母 -- “f” 和“l” (大小写均可).

In Java, the definition of basic type variables is much the same as in C. Java not only contains C and has an extension, such as the range of -128~127 "byte" type, indicating the true and false "boolean" type, do not define the array "String" type. In addition, the definitions of "float" and "long" in Java need to be followed by a letter representing the type - "f" and "l" (either case).

二、输入输出的异同

在C中输入数据时, 先定义, 后用scanf()语句输入. 输出用固定的printf()语句, 也可以调用函数,如putchar().

When you enter data in C, you define it first and then enter it with the scanf() statement. The output is a fixed printf() statement, or you can call a function such as putchar().

在Java中输入数据时, 首先需要引用java.util.Scanner, 然后定义一个Scanner型变量, 最后用定义的Scanner型变量调用nextInt()方法, 一共三步, 完成输入. 是某型就用某型的方法, 如 “float”型用nextFloat(), “String”型用next(). 此外, 第一步在该文件中只用写一次, 第二步在该类中只用写一次, 第三步则输入一个数据就写一次.

When you input data in java, you first need to reference Java.util.scanner, then define a Scanner variable, and finally use the defined Scanner variable to call nextInt() method, a total of three steps to complete the input. If it is a certain type, use a certain type of method, such as "float" type with nextFloat(), "String" type with next(). In addition, the first step is written only once in the file, the second step is written only once in the class, and the third step is written once in the input data.

在Java中输出比较多样, 可以直接调用System.out中输出方法进行输出, 可以用加号连接. 根据图中输出可知, 换行符和C一样, 为 ‘\n’. 但是像第二个一样写的话, 会进行一个ASCII码的运算, 导致输出错误. 若数据前面出现字符串, 则换行符输出正常. 所以为避免错误, 直接用println()方法, 相当与将 ‘\n’写了在方法中.

In Java, the output is more diverse, you can directly call the output method in System.out for output, you can use the plus sign connection. According to the output in the figure, it can be seen that the newline is' \n 'like C. But if you write it like the second one, an ASCII operation will be performed, resulting in an output error. If the string appears before the data, the newline output is normal. So to avoid errors, use the println() method directly, which is equivalent to writing \n in the method.

今天的分享就到这里了,

如果您对文章有独特的想法,

欢迎给我们留言。

让我们相约明天,

祝您今天过得开心快乐!

That's all for today's sharing.

If you have a unique idea about the article,

please leave us a message,

and let us meet tomorrow.

I wish you a nice day!

参考资料

翻译:网易有道翻译

本文由LearningYard学苑整理并发出,如有侵权请后台留言沟通.

文案|Dongyang

排版|Dongyang

审核|yue

Learning Yard 新学苑

标签: #js javatype