本篇适合之前学过c和c++语言,如今要学java的人进行学习。由于之前学习过c++同为面向对象的程序语言,底子语法在相对比之放学习,对比c++与java的差异,会快速掌握根本语法。学编程语言,语法不是重点,用时即查,编程思路才是重点。
1.解释、标识符、关键字、数据类型,变量定义,运算符与c++根本一致
- public class HelloWorld {
- public static void main(String[] args) {
- int a=1;
- // int b=2;
- char c='1';
- long d;
- }
- }
复制代码 int、char等定义变量,
//作为解释符号
2.数组的创建差别
- public class HelloWorld {
- public static void main(String[] args) {
- int[]arr2={11,12,13};//定义时初始化
- int []arr1;//定义后再初始化
- arr1=new int[]{11,12,13};
- int [][]arr3={{11,12},{21,11}};//二维数组
- }
- }
复制代码 对比c++- #include<iostream>
- using namespace std;
- int main()
- {
- int arr[] = { 11,12,13 };//定义时初始化
- int arr1[4];//定义后初始化
- arr1[0] = 11;
- arr1[1] = 12;
- arr1[2] = 13;
- int *arr2 = new int[n];
- int arr2[1][3];//二维数组
- int arr3[2][2] = { {11,12} ,{21,11} };
- }
复制代码 对比中可以看出区别,定义时初始化只是将[ ]的顺序变了,定义后初始化java和c++都可以用new来定义动态数组,c++静态数组的话必须在初始化时在[ ]中输入初始值。
3.输入输出与c++稍有差别
java的输出
- public class HelloWorld {
- public static void main(String[] args) {
- int a=123;
- System.out.println("hello,world");
- System.out.print("Hello, World!"+a);
-
-
- }
- }
复制代码 输出结果为- hello,world
- Hello, World!123
复制代码 System.out.println()输出后主动换行
System.out.print()少了ln后输出不会换行
IDEA中输入 sout+回车 就能快速打出System.out.println();
对比c++的输出
[code]#includeusing namespace std;int main(){ int a=123; cout |