• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Declaration of variables

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).

 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default value for a boolean is false.
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15490
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic