Java: Amazing Interesting and Cool Tricks


Java:  Amazing Interesting and Cool Tricks Part -1 πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡✌

Java☝:: A most beautiful and powerful programming languageπŸ‘Œ.

as much as you'll deep dive into Java you'll learn something new.

java cool tricks


Here are some Amazing and cool trick's - 

1-  

Do you know what will be the output of the below statement? πŸ‘‡

System.out.println(0123); 

o/p = 83   but why?

Click on Below - πŸ‘‡πŸ‘‡πŸ‘‡

System.out.println(0123);



2- Java String

Concatenation - concat() Vs the + Operator 

concat() --

if want to concat str1 with str2 where str1 is null.

str1 = null;
str2 = "javaoneworld";

str1.concat(str2);

you'll get NullPointerExceptionπŸ‘ŽπŸ‘Ž.

+ Operator --

if want to concat str1 with str2 where str1 is null.

str1 = null;
str2 = "javaoneworld";

str1+str2;

you'll not get NullPointerException instead you'll get some outputπŸ˜…πŸ˜€.

o/p - nulljavaoneworld


2-intern()


public class Javaoneworld {
    public static void main(String args[]) {
      
      String str1 = new String ("javaoneworld"); // 
      String str2  = new String ("javaoneworld");

/*
	 * as we initializing str1 and str2 with new keyword so they will not share the
	 * same memory pool
	 */
      System.out.println(str1 == str2); //false
      
      /*
	 * using the intern() method we can force both the string to share same memory
	 * pool
	 */
      
      str1 = str1.intern();
      str2 = str2.intern();
      
      System.out.println(str1 == str2); //true
    }
}
   

2- Comment

As we all know we use comments to not execute the code, 
where we use comment, compiler skips that line.

In java we have two types of comment

//single line comment

/* multi line
comment*/

But what if I want to execute the line which is even commented?

can we achieve this?

Yes , here is a way to achieve thisπŸ‘‡πŸ‘‡



public class Javaoneworld {
    public static void main(String args[]) {
      
     // \u000d System.out.println("Javaoneworld");
     
    }
}
   
This comment will execute successfully because of Unicode character that we appended, java compiler will parses this “\u000d” Unicode character as a new line. Java allows using Unicode characters without encoding.



1 comment:

  1. Thank you for sharing these tricks. This is such a great resource that you are providing and give it away for free. I love seeing blog that understand the value of providing a quality resource for free. Great blog. Data structures and algorithms in Java solutions

    ReplyDelete