• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Declaration of variables

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I want to use default value of a variable,Is it a problem if I don't declare it?(Variable of class not of method)

Example


The result is same for both variables(declared and undeclared).

 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:The default value for a boolean is false.



Yes.

So if we want to use default value of a variable there is no need to assign it.Right?
 
Carey Brown
Bartender
Posts: 5899
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is perfectly acceptable to use the default value and not explicitly specify it. All Java programmers need to know what the defaults are.
 
Carey Brown
Bartender
Posts: 5899
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loosely speaking, you can think of all of the defaults as being some form of zero. For instance a reference of null refers to no object. False is often thought of as zero'ish because historically that is how many programming languages implement a boolean state. For Java, the actual representation of true and false is up to the JVM implementation and may very well have nothing to do with zero.
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
My doubt is clarified.

I am beginner and am reading Head First Java.
I think I'll complete it in 15 days.
Can you please refer me what to learn after(while) reading the book.
 
Carey Brown
Bartender
Posts: 5899
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Loosely speaking, you can think of all of the defaults as being some form of zero. For instance a reference of null refers to no object. False is often thought of as zero'ish because historically that is how many programming languages implement a boolean state. For Java, the actual representation of true and false is up to the JVM implementation and may very well have nothing to do with zero.


Enums are an exception to this generalization in that an initial value MUST be specified or you'll get a compiler error.
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am beginner and am reading Head First Java.
I think I'll complete it in 15 days("I have watched tutorials").
Can you please refer me what to learn after(while) reading the book to become better in java.
 
Carey Brown
Bartender
Posts: 5899
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hari Nagarjuna wrote:Can you please refer me what to learn after(while) reading the book.


Two sites I can think of off-hand for programming exercises and puzzles to solve are: Project Euler, and HackerRank (sorry, no URL).

The other suggestion I'd have is to come up with some of your own projects to implement. Things that might interest you or that would become a useful tool. As an example: write a program that will recursively search a directory for all Java source code files and open each in turn and search for the occurrence of some text. Print the file path and name and the line.

I would initially avoid GUI's in the beginning and Concentrate on a textual user interface. Scanner has a lot to offer in this regard but has some nasty pitfalls that aren't well documented. Some end up developing their own utility class to simplify implementation of a TUI. (See: ScannerUtility.

One I did for myself was a rudimentary Java code beautifier, or more like an 'indenter'. See: PoorMansCodeBeautifier. If you are not using an IDE like Eclipse, which has a built in beautifier, then this utility would get you 85% there.

Bottom line: You'll need lots of practice reading as well as writing code.

 
Saloon Keeper
Posts: 10302
217
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Enums are an exception to this generalization in that an initial value MUST be specified or you'll get a compiler error.


No, there are no exceptions. Enums are reference types and therefore their default value is null.

Final fields need to have their initial value specified explicitly, either in an initializer, or in a constructor.
 
Marshal
Posts: 64622
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hari Nagarjuna wrote:. . . I think I'll complete it in 15 days("I have watched tutorials"). . . .

If you go through the book that quickly, you won't actually learn anything. Look at this article by Norvig.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!