在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:in28minutes/java-cheat-sheet开源软件地址:https://github.com/in28minutes/java-cheat-sheet开源编程语言:开源软件介绍:Java Tutorial For Beginners - A Cheat SheetReview Java 9 Concepts at Jet Speed. Complete Java CourseIntroductionBackgroundPopularity of Java
Platform Independence
JDK vs JVM VS JRE
ClassLoader
Three Types
Order of execution of ClassLoaders
First Java Programpublic class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
} Notes
Using Java and JavaCThere are two steps involved in running a Java Program
CompilationWe use javac to compile java code.
Execution
Class and Object
Variables
int number;
number = 5;
System.out.println(number);//5
number = number + 2;
System.out.println(number);//7
number = number + 2;
System.out.println(number);//9 Declaring and Initializing Variables
Tips
Primitive VariablesVariables that store value. Java defines few types like int (numbers), float(floating point numbers), char (characters). Variables of these types store the value of the variable directly. These are not objects. These are called primitive variables. An example is shown below: Primitive Variables contains bits representing the value of the variable. int value = 5; Different primitive types in java are char, boolean, byte, short, int, long, double, or float. Because of these primitive types, Java is NOT considered to be a pure objected oriented language. Numeric Data Types
char Data Type
Examples int i = 15;
long longValue = 1000000000000l;
byte b = (byte)254;
float f = 26.012f;
double d = 123.567;
boolean isDone = true;
boolean isGood = false;
char ch = 'a';
char ch2 = ';'; Reference VariablesAnimal dog = new Animal(); The instance of new Animal - Animal object - is created in memory. The memory address of the object created is stored in the dog reference variable. Reference Variables contains a reference or a guide to get to the actual object in memory. PuzzlesAnimal dog1 = new Animal();
dog1 = new Animal(); What will happen? Two objects of type Animal are created. Only one reference variable is created. Animal animal1 = new Animal();
Animal animal2 = new Animal();
animal1 = animal2; What will happen? What would happen if the same was done with primitive variables? IdentifiersNames given to a class, method, interface, variables are called identifiers. Legal Identifier Names
Java KeywordsList of Java Keywords
LiteralsAny primitive data type value in source code is called Literal. There are four types of literals:
LiteralsInteger Literals
Long Literals
Floating point Literals
Boolean Literals
Character Literals
Puzzlesint eight = 010;
int nine=011;
int invalid = 089;//COMPILER ERROR! 8 and 9 are invalid in Octal
int sixteen = 0x10;
int fifteen = 0XF;
int fourteen = 0xe;
int x = 23,000;
long a = 123456789l;
long b = 0x9ABCDEFGHL;
long c = 0123456789L;
float f = 123.456;//COMPILER ERROR! A double value cannot be assigned to a float.
boolean b = true; boolean b=false;
boolean b = TRUE;//COMPILATION ERROR
boolean b = 0; //COMPILER ERROR. This is not C Language
char ch = a;
char a = 97;
char ch1 = 66000; //COMPILER ERROR! Tip - Assignment OperatorAssignment operator evaluates the expression on the right hand side and copies the value into the variable on the left hand side. Basic Examplesint value = 35;//35 is copied into 35
int squareOfValue = value * value;//value * value = 35 * 35 is stored into squareOfValue
int twiceOfValue = value * 2; Puzzlesint a1 = 5;
int b1 = 6;
b1 = a1; // value of a1 is copied into b1
a1 = 10; // If we change a1 or b1 after this, it would not change the other variable.. b1 will remain 6
Actor actor1 = new Actor();
actor1.setName("Actor1");
//This creates new reference variable actor1 of type Actor new Actor() on the heap assigns the new Actor on the heap to reference variable
Actor actor2 = actor1;
actor2.setName("Actor2");
System.out.println(actor1.getName());//Actor2 Casting - Implicit and ExplicitCasting is used when we want to convert one data type to another.
Implicit Casting
byte b = 10; //byte b = (int) 10; Example below compiles because compiler introduces an implicit cast.
short n1 = 5;
short n2 = 6;
//short sum = n1 + n2;//COMPILER ERROR
short sum = (short)(n1 + n2);//Needs an explicit cast
byte b = 5;
b += 5; //Compiles because of implicit conversion
int value = 100;
long number = value; //Implicit Casting
float f = 100; //Implicit Casting Explicit Casting
long number1 = 25678;
int number2 = (int)number1;//Explicit Casting
//int x = 35.35;//COMPILER ERROR
int x = (int)35.35;//Explicit Casting
int bigValue = 280;
byte small = (byte) bigValue;
System.out.println(small);//output 24. Only 8 bits remain.
//float avg = 36.01;//COMPILER ERROR. Default Double
float avg = (float) 36.01;//Explicit Casting
float avg1 = 36.01f;
float avg2 = 36.01F; //f or F is fine
//byte large = 128; //Literal value bigger than range of variable type causes compilation error
byte large = (byte) 128;//Causes Truncation! Compound Assignment Operators
int a = 5;
a += 5; //similar to a = a + 5;
a *= 10;//similar to a = a * 10;
a -= 5;//similar to a = a - 5;
a /= 5;//similar to a = a / 5; Other OperatorsRemainder(%) Operator
System.out.println(10 % 4);//2
System.out.println(15 % 4);//3
System.out.println(-15 % 4);//-3 Conditional Operator
int age = 18;
System.out.println(
age >= 18 ? "Can Vote": "Cannot Vote");//Can Vote
age = 15;
System.out.println(
age >= 18 ? "Can Vote": "Cannot Vote");//Cannot Vote Bitwise Operators
System.out.println(25|12);//output will be 29
/*convert to binary and calculate:
00001100 (12 in decimal)
00011001 (25 in decimal)
________
00011101 (29 in decimal) */
System.out.println(25&12);//output will be 8
System.out.println(25^12);//output will be 21 Passing Variables to Methods
Passing Variables to Methods : Example
Returning a Value From Method
Types of Variables
Instance Variables
Local Variables
Member Variables
Static Variable
Member Variable and Static Variable
Example Static and Member Variablespublic class StaticAndMemberVariables {
public static void main(String[] args) {
Actor actor1 = new Actor();
actor1.name = "ACTOR1";
//Actor.name //Compiler Error
//Below statement can be written as actor1.count++
//But NOT recommended.
Actor.count++;
Actor actor2 = new Actor();
actor2.name = "ACTOR2";
//Below statement can be written as actor2.count++
//But NOT recommended.
Actor.count++;
System.out.println(actor1.name);//ACTOR1
System.out.println(actor2.name);//ACTOR2
//Next 3 statements refer to same variable
System.out.println(actor1.count);//2
System.out.println(actor2.count);//2
System.out.println(Actor.count);//2
}
}
class Actor {
//RULE 1: Member Variables can be accessed
//only through object references
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论