OCJP REFERENCES

Java Packaging:

https://stackoverflow.com/questions/2193226/how-to-import-a-class-from-default-package

https://docs.oracle.com/javase/specs/#7.4.2

http://www.xyzws.com/javafaq/what-is-the-default-package/126

https://stackoverflow.com/questions/9729197/getting-current-class-name-including-package

https://coderanch.com/t/270542/certification/Package-statement-Import-statement

https://stackoverflow.com/questions/3179216/what-is-the-convention-for-word-separator-in-java-package-names?rq=1

https://stackoverflow.com/questions/6532726/java-package-politics?rq=1

https://www.intertech.com/Blog/whats-package-info-java-for/

https://stackoverflow.com/questions/12599058/eclipse-juno-what-is-the-use-of-package-info-java

https://www.leepoint.net/language/10basics/import.html

 

Imports in java:

http://www.studytonight.com/java/package-in-java.php

I have static import feature a lot while using unit testing and mocking libraries like Mockito, JUnit, TestNG and others. You can static import key class e.g.

import static org.junit.Assert.*
import static org.mockito.Mockito.*
import static org.mockito.Matcher.*

You can even configure Eclipse content assist to show statically imported method, without even them manually imported, by configuring them in favorites under content assist. Next time, if those libraries in your classpath, typing assert will show all assert methods from jUnit and so on.

Read more: http://javarevisited.blogspot.com/2012/10/what-is-static-import-in-java-5-example-tutorial.html#ixzz4wAxuGppM

https://stackoverflow.com/questions/29382728/constant-interface-anti-pattern-clarification

 

 

 

 

 

Advertisements

Identifiers in JAVA

  • All the java components like  classes,interfaces,methods,variables,packages etc needs names in order to make benefit from them in our program .These names are called “identifiers”.
  • They are used to refer the actual reference value assigned to any variable etc.
  • Example:

 If i declare
String name=”India”;
int value=10;

Here name is a identifier of a class called java.lang.String;
Here value is an identifier which represents a primitive data type of int .It is having 10 as it’s value.

  • In the HelloWorld program, HelloWorld,String- HelloWorld, args, main and println are respresents methods. 

 

Legal Identifiers:

The set of rules of the compiler to determine whether a name is legal or not.Legal identifiers are must be composed of following combinations.They are

  • Numbers(0-9)
  • Currency Symbols($ as JAVA supports)
  • Connecting Characters(_)
  • Unicode characters(A_Z ,a-z,$,_)

Example:

public class Identifiers{
public static void main(String[] args)throws Exception{
int _a=10;
int _________abc_bc=10;
int abc_____455_dfj=10;
int a#=10;
int a$=10;
int $res=10;
int &iop=10;
int 34_u=10;
int char=10;
System.out.println(_a);
System.out.println(_________abc_bc);
System.out.println(abc_____455_dfj);
System.out.println(a#);
System.out.println(a$);
System.out.println($res);
System.out.println(&iop);
System.out.println(34_u);
System.out.println(char);
}

output:

D:\OCJP>javac Identifiers_Test.java
Identifiers_Test.java:6: error: illegal character: ‘#’
int a#=10;
^
Identifiers_Test.java:9: error: not a statement
int &iop=10;
^
Identifiers_Test.java:9: error: ‘;’ expected
int &iop=10;
^
Identifiers_Test.java:10: error: illegal underscore
int 34_u=10;
^
Identifiers_Test.java:10: error: not a statement
int 34_u=10;
^
Identifiers_Test.java:11: error: not a statement
int char=10;
^
Identifiers_Test.java:11: error: ‘;’ expected
int char=10;
^
Identifiers_Test.java:11: error: not a statement
int char=10;
^
Identifiers_Test.java:11: error: ‘;’ expected
int char=10;
^
Identifiers_Test.java:15: error: illegal character: ‘#’
System.out.println(a#);
^
Identifiers_Test.java:15: error: illegal start of expression
System.out.println(a#);
^
Identifiers_Test.java:18: error: illegal start of expression
System.out.println(&iop);
^
Identifiers_Test.java:19: error: illegal underscore
System.out.println(34_u);
^
Identifiers_Test.java:19: error: ‘)’ expected
System.out.println(34_u);
^
Identifiers_Test.java:19: error: illegal start of expression
System.out.println(34_u);
^
Identifiers_Test.java:20: error: ‘.class’ expected
System.out.println(char);
^
Identifiers_Test.java:21: error: reached end of file while parsing
}
^
17 errors

so by above program we came to know that we should fallow some rules in order to make it as an identifier.

Rules of Legal Identifiers:

  • It shouldn’t start with digit(0-9).
  • It shouldn’t  start with special charactes like (@ # %^&*()!)
  • It shouldn’t contain the keyword i.e “char” “int”
  • It can have prefered length. “ppppppp______dfds_dsgfdg”.
  •  It shouldn’t contain white spaces.
  • _ can be first letter.$ can be used in midddle.Apart from no special charactes are allowd.

Now i have removed all the invalid identifier declarations .It is working fine as expected.

public class Identifiers_Test{
public static void main(String[] args)throws Exception{
int _a=10;
int _________abc_bc=10;
int abc_____455_dfj=10;
int a_=10;
int a$=10;
//int a#=10;
//int $res=10;
//int &iop=10;
//int 34_u=10;
//int char=10;
System.out.println(_a);
System.out.println(_________abc_bc);
System.out.println(abc_____455_dfj);
//System.out.println(a#);
System.out.println(a$);
System.out.println(a_);
//System.out.println(a#);
//System.out.println($res);
//System.out.println(&iop);
//System.out.println(34_u);
//System.out.println(char);
}
}

output:

D:\OCJP>javac Identifiers_Test.java

D:\OCJP>java Identifiers_Test
10
10
10
10
10

D:\OCJP>

 

Why we should follow identifier rules:

  • Conflicts like int a-=10; If we declare like this compile got confused to choose whether it is a short hand operator or identifier.