Monday, 30 May 2016
loops
Types of loops
for(initialization;termination;update):
1)initialization allows only assignment.
2)termination condition must return a boolean.
3)update: allows assignment, pre- and post operations, and method call, even new class instances; but logic comparing is allowed.
for(element:collection):
for(Iterator it;it has next)
do{}while(termination);
while(termination){do};
Keyword:
break: quite from the whole of loop.
continue:quite from current iteration.
Note: 1) 'break' used only in switch and loop; 2) 'continue' only used in the loop.
Switch(x) Rules
Java really have many rules, that is why it is the most hard lang. to dominate.
In java, x can only be
1) primitive type: byte, short, char, int
2) and their wrapper classes.
3) reference type: String and Enum.
please note: x cannot be boolean, long, float, and double.
5) x can be an expression, like a+b etc; but it must return types above.
6) Case values: a) the value of a case label must be compiling-time constant. the following case is not valid. however, you may make variable final, for the final variable won't be changed once it is declared and initialized. I guess the reason it is easy to understand. On compiling time, the case number are variables, then how to determine the case brunches?
the following is valid, for b and c are constants on compiling time.
how about the following case? it is not considered as compiling time constants, for the constant a and b, not initialized as declaring.
b) null cannot be case number. c) case number should have the same type as switch input type.
(7) switch body can only contain case and default brunch
(8) case type must fit the type of x
(9) case cannot be duplicated.
(10) x cannot be Object type, even though x could be String and Enum
(11) when x matches a case; it will be handled there; when a case has no break then it will falling down to implement the rest cases until a break is met.
(12) default can have break or not have; however, if default is located before or among cases, it should have a break.
Actually, I don't know why switch has been created so complicated; more than necessary?
In java, x can only be
1) primitive type: byte, short, char, int
2) and their wrapper classes.
3) reference type: String and Enum.
please note: x cannot be boolean, long, float, and double.
5) x can be an expression, like a+b etc; but it must return types above.
6) Case values: a) the value of a case label must be compiling-time constant. the following case is not valid. however, you may make variable final, for the final variable won't be changed once it is declared and initialized. I guess the reason it is easy to understand. On compiling time, the case number are variables, then how to determine the case brunches?
int a=10, b=20, c=30;
switch (a) {
case b+c: System.out.println(b+c); break;
case 10*7: System.out.println(10*7512+10); break;
}
the following is valid, for b and c are constants on compiling time.
final int a = 10;
final int b = 20;
final int c = 30;
switch (a) {
case b+c: System.out.println(b+c); break;
}
how about the following case? it is not considered as compiling time constants, for the constant a and b, not initialized as declaring.
public class VarCase {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final int a, b;
a = 20;
b = 10;
int x = a + b;
switch (x) {
case a + b:
System.out.println("" + (a + b));
break;
}
}
}
b) null cannot be case number. c) case number should have the same type as switch input type.
(7) switch body can only contain case and default brunch
(8) case type must fit the type of x
(9) case cannot be duplicated.
(10) x cannot be Object type, even though x could be String and Enum
(11) when x matches a case; it will be handled there; when a case has no break then it will falling down to implement the rest cases until a break is met.
(12) default can have break or not have; however, if default is located before or among cases, it should have a break.
Actually, I don't know why switch has been created so complicated; more than necessary?
Sunday, 29 May 2016
anagram
I am thinking of this ........... How to find out Permutations of a string
"TEAM"
"T" "E" "A" "M" //1
taking "T" and removing it from //1
"E" "A" "M" //2
"T" //3
combination of //2 and //3
"TE" "TA" and "TM"
Saturday, 28 May 2016
Object eligible for GC
1) When an object become no long accessed, for instance outside of its scope.
2) When an object reference variable is assigned a null, or re-assigned another object reference; so the object may become eligible for GC, for it might be not ref. any more.
Concept:
reference var. holding the reference value.
instance: it is instantiated in the heap memory by using new keyword, or specific case for the string using "hello world".
reference: more like the pointer in the C programming language; it points to a memory location of the instance.
Java passing by value:
When assigning an instance to a reference variable, its reference is copied to the reference variable.
When an instance has not been referred by any reference variable, then it is eligible to be released for saving resource. This is automatically by the garbage collector.
It is possible to explicitly declare a reference variable to be null, thus dissolving the binding with the instance. It is a way to release the instance, and become eligible to cleaned.
It is possible to explicitly force GC to do garbage collection, by using System.gc() command. However, the impact is not guaranteed immediately.
Scope of Java Variables
Variable life span depends on its scope. Only 4 scopes:
1) Class variable //1 once class loaded, accessible by all instances of class
2) Instance variable //2 once instance constructed as calling its constructor.
3) Method argument variable //3 once method is invoked
4) Local variable //4 being alive within the method alone, or a block.
//1 outside all instances of the class; accessible by all instances method and class methods.
//2 outside all instance methods, can be accessible by them alone.
//3 method argument variable has a method scope.
//4 within in a scope of method, or a block, for example a if(){ }
The scope of a local variable depends its declaration place within the method
Please note: instance variable cannot be accessible in a static context, i.e. by a class method.
Some rules:
(1) Class and instance var. should not be declared as the same name;(compiling error).
(2) Local var. and method parameter should not have the same name.(compiling error)
(3) Local var. can hide instance var and class var.
(4) Local var. must init. before using it, otherwise a compiling error.
1) Class variable //1 once class loaded, accessible by all instances of class
2) Instance variable //2 once instance constructed as calling its constructor.
3) Method argument variable //3 once method is invoked
4) Local variable //4 being alive within the method alone, or a block.
//1 outside all instances of the class; accessible by all instances method and class methods.
//2 outside all instance methods, can be accessible by them alone.
//3 method argument variable has a method scope.
//4 within in a scope of method, or a block, for example a if(){ }
The scope of a local variable depends its declaration place within the method
Please note: instance variable cannot be accessible in a static context, i.e. by a class method.
/**
*
* @author YNZ
*/
public class BlockScope {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
boolean b = false; //method scope
if (b) {
int i = 0; //block scope
} else {
int i = 1; //block scope
}
}
}
Some rules:
(1) Class and instance var. should not be declared as the same name;(compiling error).
(2) Local var. and method parameter should not have the same name.(compiling error)
(3) Local var. can hide instance var and class var.
(4) Local var. must init. before using it, otherwise a compiling error.
/**
*
* @author YNZ
*/
public class LocalVar {
static int i; //class var.
public static void main(String[] args) {
//int i=0; local var. must be init. before using it.
//local var. hiding the class field i.
//Well, this is a java feature. this is not an error.
for (int i = 0; i < 10; i++) { //local var. i within scope of for loop.
System.out.print(" " + Math.random());
}
System.out.println("");
for (int i = 0; i < 10; i++) {//local var. i with scope of for loop.
System.out.print(" " + Math.random());
}
System.out.print("\n");
}
public void m() {
int d; //method variable(local var.)
//d++, it must be init. before using it; otherwise it gives comipling error.
{
{
}
};
}
public void m(int x) {
int y = 0; //int x =0 will cause an error
if (true) {
int z = 0; //life span within if block
} else {
int z = 100; //life span within else block
}
int z =300; //so here it is valid.
System.out.println(z);
}
}
3 ways to create String instance
It may be tricky if not knowing the concept clearly.
There are three ways to create String instances in java. They may be instantiated in different part of memory allocation, i.e. heap memory or string constant pool; in the constant pool, an string instance may be reused for memory efficiency.
1) String str = new String("hello world"); //1
2) String str = "hello world"; //2
3) System.out.println("hello world"); //3
//1: "new" will instantiate a new string object in heap memory, no matter what.
//2: JVM will check if the string constant already created in the constant pool first; if not, then create a new one; otherwise, the existing will be reused, so the reference of the same string maybe copied to different String variables.
//3: "xxxxx" may also instantiate a new string object in the string constant pool, if the same text constant is not existed yet.
Tuesday, 24 May 2016
Java Access Modifier
public: anywhere.
protected: accessible by classes within in the same package, and sub-class outside the package.
default: only access within the class and classes in the same package.
private: only access within the class, not accessible even by the sub-classes.
The following example will show a compiling error, for A constructor using default modifier cannot be accessible outside the package. So it causes sub-class to init. error as calling super().
//File A.java
package a;
public class A {
A() {
}
public void print() {
System.out.println("A");
}
}
//File B.java
package b;
import a.*;
public class B extends A {
B() {
}
public void print() {
System.out.println("B");
}
public static void main(String[] args) {
new B();
}
}
class normally being declared as public or default accessible.private class is invalid modifier for the class, actually it doesn't make any sense; a private class should be declared as an inner class.
Subscribe to:
Posts (Atom)
Can Jackson Deserialize Java Time ZonedDateTime
Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...
-
The strictfp keyword is used to force the precision of floating point calculations (float or double) in Java conform to IEEE’s 754 standard...
-
In construction. Spring test annotations: @SpringBooTest @DataJpaTest @TestPropertySource @ActiveProfiles @Sql @SpringBootTest It is used f...
-
Testing Controllers @Runwith(SrpingRunner.class) Unit test @Runwith(SrpingRunner.class) @WebMvcTest(SpecificController.class) Integration te...