package helloworldapp;
import java.util.*;
public class language_basics {
/** Variables */
int speed = 0; //local variable
static int numGers = 6; //global int
final static int pp = 3; //global not changeable variable int
// primitive data types
byte gear = 1;
int g1 = 1;
short g2 = 1;
long g3 = 1;
float g4 = 1;
double g5 = 1;
boolean g6 = false;
char g7 = 'o';
String g8 = "helloworld";
// integer literals
int decVal = 26;//decimal
int hexVal = 0x1a;//hexadecimal
int binVal = 0b11010;//binary
// floating point literals
double d1 = 123.4;
double d2 = 1.234e2;
float f1 = 123.4f;
/** Arrays */
int[] anArray = new int[10]; // declare a non-size array and allocate memory for 10 integers
int[] newArray = {100, 200, 300,
400, 500, 600,
700, 800, 900, 1000};
String[][] names = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}
};
int a1 = newArray[1];
char[] copyForm = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd'};
/** Arrays method */
public int copyArray(){
char[] copyTo = Arrays.copyOfRange(copyForm,2,9);
System.out.println(new String(copyTo));
return 1;
}
public void getIdx(){
int idx = Arrays.binarySearch(copyForm,'a');
System.out.println(idx);
}
public boolean equalArray(){
return Arrays.equals(copyForm,new char[]{'a'});
}
public void putVal(){
Arrays.fill(copyForm,0,2,'a');
}
public void sortArray(){
Arrays.parallelSort(copyForm);
}
/** Operators */
int result = 1+2;
// basic arithmetic operators
private void calc(){
result -= 1;
result--;
result++; // evaluate origin value
++result; // evaluate the plus 1 value
result *= 2;
result /= 2;
result %= 2;
String a = "hellow" + "orld";
boolean e = !true;
}
// condition operators
private void condition(){
int v1 = 1;
int v2 = 2;
if (v1 == v2)
System.out.println(v1);
if (v1!=v2)
System.out.println(v1);
if ((v1==2) && (v2==1))
System.out.println(v1);
if ((v1==2) || (v2==1))
System.out.println(v1);
int res = v1==v2?v1:v2; //condition assignment
}
// type comparison: instanceof
class Parent{}
interface MyInterface{}
class Child extends Parent implements MyInterface{}
private void typeCompare(){
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println("obj1 instanceof Parent: "
+ (obj1 instanceof Parent)); //true
System.out.println("obj1 instanceof Child: "
+ (obj1 instanceof Child)); //false
System.out.println("obj1 instanceof MyInterface: "
+ (obj1 instanceof MyInterface)); //false
System.out.println("obj2 instanceof Parent: "
+ (obj2 instanceof Parent)); //true
System.out.println("obj2 instanceof Child: "
+ (obj2 instanceof Child)); //true
System.out.println("obj2 instanceof MyInterface: "
+ (obj2 instanceof MyInterface)); //true
}
/** control flow */
void ifelse(){
int i = 76;
String score;
if (i >= 73){
score = "good";
}
else if (i >= 80){
score = "very good";
}
else {
score = "excellent";
}
}
void switchcase(){
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
}
void dowhile(){
int i = 3;
while (i>0){
i--;
return; // exit the method completely
}
do{i--;}while(i>0);
}
void forloop(){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
continue;
}
int[] numbers =
{1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
break; // unlabeled break --> another is labeled break(need label outside loop)
}
}
}
联系方式:clarence_wu12@outlook.com