
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Do local variables in Java have default values?
No, local variables do not have default values. Once we create a local variable we must initialize it before using it.
Since the local variables in Java are stored in stack in JVM there is a chance of getting the previous value as default value. Therefore, In Java default values for local variables are not allowed.
Example
public class Sample { public static void main(String args[] ){ int data; System.out.println(data); } }
Error
C:\Sample>javac Sample.java Sample.java:4: error: variable data might not have been initialized System.out.println(data); ^ 1 error
- Related Questions & Answers
- Local variables in Java
- Member variables vs Local variables in Java
- Global and Local Variables in Java
- Default values of static variables in C
- How are C++ Local and Global variables initialized by default?
- What are class variables, instance variables and local variables in Java?
- Do static variables get initialized in a default constructor in java?
- Are static local variables allowed in Java?
- System variables vs Local Variables in MySQL?
- Why we do not have global variables in C#?
- Final local variables in C#
- User-defined variables vs Local Variables in MySQL?
- Default array values in Java
- Can a method local inner class access the local final variables in Java?
- What is the scope of local variables in Java?
Advertisements