PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 10, 2022

[FIXED] Why aren't Java references not null by default?

 July 10, 2022     java, reference     No comments   

Issue

I've seen this question for other languages (C and C++). But I still don't get why it is like this in Java as well. Where could it be useful that a reference is declared but not set to null?


Solution

Thing ref;
if (someCondition)
   ref = oneThing;
else 
   ref = anotherThing;

There is no benefit in initializing 'ref' to null in the above code, at least not as long as neither assignment can throw an exception.

It's not "useful" that it is uninitialized, it's merely that there's no point in initializing it.

I wish it were not like that - I'd prefer initialization of local variables to work like member variables - but that is how it is.

I assume it's for efficiency reasons. If you don't have to initialize local variables, allocation is pretty much just an adjustment of the stack pointer.



Answered By - user16632363
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing