变量类型
变量声明
变量声明例子:
int a,b,c;
int a=10,b=6;
char a='a';
变量类型
| 变量类型 | 备注 |
|---|---|
| 本地变量 | 在方法、构造器或块中声明;本地变量没有默认值,必须被声明且在第一次使用时要给它赋值 |
| 实例变量 | 在类中声明,在方法、构造器外;实例变量具有默认值;在类中可以叫名字访问 |
| 静态(类)变量 | 在类中用static声明,在方法、构造器外;每个类只有一个类变量;具有默认值;当静态变量被作为** public static final **声明时,变量(常量)名称都要用大写字母 |
基本运算符
| 运算符 | 详注 |
|---|---|
| 算数运算符 | +;-;* ;/(取除数,为整数) ;%(取余数,为整数) |
| 关系运算符 | ==;!=;>;<;>=;<= |
| 位运算符 | 应用于整数型;&(and);|(or); ^(xor);~(not);<<(左移运算符);>>(右移运算符);>>>(右移补零运算符); |
| 逻辑运算符 | &&(与);||(或);!(非) |
| 条件运算符 | variable x = (expression) ? value if true : value if false ; 举例:b = (a == 1) ? 20: 30; |
| instanceof 符 | 举例: boolean result = name instanceof String; |
循环控制
while 循环
在执行时,如果布尔表达式的结果为真,则循环中的动作将被执行。只要该表达式的结果为真,执行将继续下去.
while(Boolean_expression)
{
//Statements
}
for 循环
- 普通情况
可以有效地编写需要执行的特定次数的循环
for(initialization; Boolean_expression; update)
{
//Statements
}
- 数组foreach
for(declaration : expression)
{
//Statements
}
举例: for(int x : numbers )
关键字
break:停止整个循环;
continue: 停止本次循环。
条件判断
if else
语法
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
//Executes when the Boolean expression 3 is true
}else {
//Executes when the none of the above condition is true.
}
switch
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
本文全面介绍了编程基础概念,包括变量类型、声明、基本运算符、循环控制、关键字及条件判断等核心内容,适合初学者快速掌握编程基础知识。
3万+

被折叠的 条评论
为什么被折叠?



