java学习——底子语法篇(适合已经学过c++的人群)

络腮胡菲菲  金牌会员 | 2024-7-6 11:40:29 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 903|帖子 903|积分 2709

本篇适合之前学过c和c++语言,如今要学java的人进行学习。由于之前学习过c++同为面向对象的程序语言,底子语法在相对比之放学习,对比c++与java的差异,会快速掌握根本语法。学编程语言,语法不是重点,用时即查,编程思路才是重点。

1.解释、标识符、关键字、数据类型,变量定义,运算符与c++根本一致
  1. public class HelloWorld {
  2.     public static void main(String[] args) {
  3.         int a=1;
  4. //        int b=2;
  5.         char c='1';
  6.         long d;
  7.     }
  8. }
复制代码
int、char等定义变量,
//作为解释符号
2.数组的创建差别
  1. public class HelloWorld {
  2.     public static void main(String[] args) {
  3.         int[]arr2={11,12,13};//定义时初始化
  4.         int []arr1;//定义后再初始化
  5.         arr1=new int[]{11,12,13};
  6. int [][]arr3={{11,12},{21,11}};//二维数组
  7.     }
  8. }
复制代码
对比c++
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.         int arr[] = { 11,12,13 };//定义时初始化
  6.         int arr1[4];//定义后初始化
  7.         arr1[0] = 11;
  8.         arr1[1] = 12;
  9.         arr1[2] = 13;
  10. int *arr2 = new int[n];
  11.         int arr2[1][3];//二维数组
  12.         int arr3[2][2] = { {11,12} ,{21,11} };
  13. }
复制代码
对比中可以看出区别,定义时初始化只是将[ ]的顺序变了,定义后初始化java和c++都可以用new来定义动态数组,c++静态数组的话必须在初始化时在[ ]中输入初始值。
3.输入输出与c++稍有差别

java的输出
  1. public class HelloWorld {
  2.     public static void main(String[] args) {
  3.   int a=123;
  4.         System.out.println("hello,world");
  5.     System.out.print("Hello, World!"+a);
  6.   
  7.     }
  8. }
复制代码
输出结果为
  1. hello,world
  2. 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
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

络腮胡菲菲

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表