Guest User

Untitled

a guest
Dec 6th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. int x;
  2. x = 10;
  3.  
  4. Integer num;
  5. num = new Integer(10);
  6.  
  7. public void doSomething(SomeObject obj){
  8. //do something to obj
  9. }
  10.  
  11. doSomething(null);
  12.  
  13. /**@param obj An optional foo for ____. May be null, in which case
  14. * the result will be ____. */
  15. public void doSomething(SomeObject obj){
  16. if(obj != null){
  17. //do something
  18. } else {
  19. //do something else
  20. }
  21. }
  22.  
  23. public class Example
  24. {
  25. public static void main(String[] args)
  26. {
  27. Object obj = null;
  28. obj.hashCode();
  29. }
  30. }
  31.  
  32. SynchronizedStatement:
  33. synchronized ( Expression ) Block
  34.  
  35. public class Printer {
  36. private String name;
  37.  
  38. public void setName(String name) {
  39. this.name = name;
  40. }
  41.  
  42. public void print() {
  43. printString(name);
  44. }
  45.  
  46. private void printString(String s) {
  47. System.out.println(s + " (" + s.length() + ")");
  48. }
  49.  
  50. public static void main(String[] args) {
  51. Printer printer = new Printer();
  52. printer.print();
  53. }
  54. }
  55.  
  56. Exception in thread "main" java.lang.NullPointerException
  57. at Printer.printString(Printer.java:13)
  58. at Printer.print(Printer.java:9)
  59. at Printer.main(Printer.java:19)
  60.  
  61. private String name = "";
  62.  
  63. printString((name == null) ? "" : name);
  64.  
  65. public class Printer {
  66. private final String name;
  67.  
  68. public Printer(String name) {
  69. this.name = Objects.requireNonNull(name);
  70. }
  71.  
  72. public void print() {
  73. printString(name);
  74. }
  75.  
  76. private void printString(String s) {
  77. System.out.println(s + " (" + s.length() + ")");
  78. }
  79.  
  80. public static void main(String[] args) {
  81. Printer printer = new Printer("123");
  82. printer.print();
  83. }
  84. }
  85.  
  86. public class Test {
  87. public static void main(String[] args) {
  88. String foo = null;
  89. int length = foo.length(); // HERE
  90. }
  91. }
  92.  
  93. $ javac Test.java
  94. $ java Test
  95. Exception in thread "main" java.lang.NullPointerException
  96. at Test.main(Test.java:4)
  97. $
  98.  
  99. Exception in thread "main" java.lang.NullPointerException
  100.  
  101. at Test.main(Test.java:4)
  102.  
  103. int length = foo.length(); // HERE
  104.  
  105. public class Test {
  106.  
  107. private static String[] foo = new String[2];
  108.  
  109. private static int test(String[] bar, int pos) {
  110. return bar[pos].length();
  111. }
  112.  
  113. public static void main(String[] args) {
  114. int length = test(foo, 1);
  115. }
  116. }
  117.  
  118. $ javac Test.java
  119. $ java Test
  120. Exception in thread "main" java.lang.NullPointerException
  121. at Test.test(Test.java:6)
  122. at Test.main(Test.java:10)
  123. $
  124.  
  125. return args[pos].length();
  126.  
  127. int length = test(foo, 1);
  128.  
  129. private static String[] foo = new String[2];
  130.  
  131. String a = null;
  132. System.out.println(a.toString()); // NullPointerException will be thrown
  133.  
  134. public class Some {
  135. private int id;
  136. public int getId(){
  137. return this.id;
  138. }
  139. public setId( int newId ) {
  140. this.id = newId;
  141. }
  142. }
  143. ....
  144. ....
  145. // Somewhere else...
  146. Some reference = new Some(); // Point to a new object of type Some()
  147. Some otherReference = null; // Initiallly this points to NULL
  148.  
  149. reference.setId( 1 ); // Execute setId method, now private var id is 1
  150.  
  151. System.out.println( reference.getId() ); // Prints 1 to the console
  152.  
  153. otherReference = reference // Now they both point to the only object.
  154.  
  155. reference = null; // "reference" now point to null.
  156.  
  157. // But "otherReference" still point to the "real" object so this print 1 too...
  158. System.out.println( otherReference.getId() );
  159.  
  160. // Guess what will happen
  161. System.out.println( reference.getId() ); // :S Throws NullPointerException because "reference" is pointing to NULL remember...
  162.  
  163. public class student {
  164. private int id;
  165. public int getId(){
  166. return this.id;
  167. }
  168. public setId( int newId ) {
  169. this.id = newId;
  170. }
  171. }
  172.  
  173. public class School
  174. {
  175.  
  176. student Obj_Student;
  177. public school()
  178. {
  179.  
  180. try
  181. {
  182.  
  183. Obj_Student.getId();
  184. }catch(Exception e)
  185. {
  186. System.out.println("Null Pointer ");
  187. }
  188. }
  189.  
  190. }
  191.  
  192. public class School
  193. {
  194.  
  195. student Obj_Student;
  196. public school()
  197. {
  198.  
  199. try
  200. {
  201. Obj_Student = new student();
  202. Obj_Student.setId(12);
  203. Obj_Student.getId();
  204. }catch(Exception e)
  205. {
  206. System.out.println("Null Pointer ");
  207. }
  208. }
  209.  
  210. }
  211.  
  212. String[] phrases = new String[10];
  213. String keyPhrase = "Bird";
  214. for(String phrase : phrases) {
  215. System.out.println(phrase.equals(keyPhrase));
  216. }
  217.  
  218. String[] phrases = new String[] {"The bird", "A bird", "My bird", "Bird"};
  219. String keyPhrase = "Bird";
  220. for(String phrase : phrases) {
  221. System.out.println(phrase.equals(keyPhrase));
  222. }
Add Comment
Please, Sign In to add comment