Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Casting "null" when and why ?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Casting "null" when and why ?

    Greeting ,,

    please , i need illustrating to line 6 of this example why do we need to cast null ? whats the benefit ?

    captured example :

    1// @Nullable
    2// public KeyStroke getKeyStroke(String key) {
    3// KeyStroke ks = null;
    4// try {
    5// String s = getStringRecursive(key);
    6// ks = (s == null) ? (KeyStroke) null : KeyStroke.getKeyStroke(s);
    7// } catch (NoSuchElementException e) {
    8// }
    9// return ks;


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Casting "null" when and why ?

    Personally I don't see any reason why you would want to cast it.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Casting "null" when and why ?

    ks = (s == null) ? (KeyStroke) null : KeyStroke.getKeyStroke(s);

    If I saw anyone write code like that they would be on the receiving end of a headslap.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  2. Replies: 6
    Last Post: November 12th, 2010, 03:40 AM
  3. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM