*
The moose likes Beginning Java and the fly likes Output analysis Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Output analysis" Watch "Output analysis" New topic
Author

Output analysis

Gokul Osho
Greenhorn

Joined: Mar 21, 2013
Posts: 14
Hi All,

Forgive me if this question sounds stupid.

I am a newbie.. Have just started learning java from "Head First Java" book.. I tried a sample program which is listed there.. pasting the program as well as my questions here..

1) when I tried placing the if loop before the while loop, the output was "DoDooBeeDooBee" whereas the above programs' output was "DooBeeDooBee". Is it because the value of x got incremented inside while loop, i.e)2 and so the if loop takes the x value as 2. Am I right?

2) Initially when i typed the program, I mentioned few words in lowercase

public static void main (String[] args) - I mentioned String as string in this line
System.out.print("Bee") - System as system in this line

This threw error.

Here, changing the first letter of the word "string" and "system" to upper case gave desired results. Is it a pattern that needs to be followed? If so, what all keywords exist out there in java which needs to be typed in certain format?


Appreciate your help.

Thanks,
Gokul
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

first, you should learn to properly indent your code. Then, you should use code tags when posting it here. Folks will find this much easier to read:



Next, it's kind of hard to follow what you mean when you simply say "i moved this to before that". Post the exact change you made. I think you mean you did this:



to answer your question...yes, x gets incremented on the line "x=x+1". each time you come to that line, x goes up by one. since there is nothing to reset it, by the time you get to the if statement, x is actually 3 (if i'm parsing it right).

To answer your second question....EVERYTHING in java is case sensitive. "fred" is never the same thing as "Fred" or "FRED". so a String is not the same thing as a string. System is not the same thing as System.

Generally, classes start with a capital letter - and String and System are both class names. Keywords are lower case. so it will always be "if" and never "If". You get used to it pretty quick.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
And welcome to the Ranch
Gokul Osho
Greenhorn

Joined: Mar 21, 2013
Posts: 14


Thanks Campbell and Fred.

@Fred: You have mentioned that class names start with a uppercase letter. And String and System are class names?!! I don't get it. Dig the web and found justification for the fact "System is a class". Trying to understand. By the way, String is a class name? Isn't it a data type? Forgive my ignorance here.

Best,
Gokul
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
System and String are class names in Java. They might mean other things in other contexts, however.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gokul Osho wrote:String is a class name? Isn't it a data type? Forgive my ignorance here.


In Java, every class is a type, although not all types are classes.

String is a class just like Object or Date or ArrayList or JPanel or MyClassThatICreatedForThisExcercise. In addition to being a class just like all those, String also gets special treatment by the language just to make writing and reading code easier, and to make some things more efficient, since it's used so much.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

also...the word 'string' is sort of overloaded. It can refer to a sequential group of characters in a generic way. You can have a string in just about any language. I first heard the term when I was learning basic back in grade school.

How languages deal with strings varies. The C programming language just made an array that held characters, with a special value marking the end. Java has a pre-built class called String that you can use to process strings. The language has special built in handling of some functions of strings, since they are so commonly used.

There is nothing that FORCES a class to start with a capital letter. You could create your own class called 'mystring'. it would work just fine. The problem is that when someone else comes along and reads your code, they expect things to be done a certain way, since that is how EVERYBODY does it. If you break these conventions, your code is harder for others to read.

If you want to get an idea of what classes come 'pre-built' for you, just check out the API. Here is the api for java 6. You should see an "All Classes" pane. Everything in there is a class (or an interface, but don't worry about that yet). You can see there is a lot already done for you. Part of becoming a java programmer is learning enough of these that you don't re-invent the wheel.
Gokul Osho
Greenhorn

Joined: Mar 21, 2013
Posts: 14
fred rosenberger wrote:also...the word 'string' is sort of overloaded. It can refer to a sequential group of characters in a generic way. You can have a string in just about any language. I first heard the term when I was learning basic back in grade school.

How languages deal with strings varies. The C programming language just made an array that held characters, with a special value marking the end. Java has a pre-built class called String that you can use to process strings. The language has special built in handling of some functions of strings, since they are so commonly used.

There is nothing that FORCES a class to start with a capital letter. You could create your own class called 'mystring'. it would work just fine. The problem is that when someone else comes along and reads your code, they expect things to be done a certain way, since that is how EVERYBODY does it. If you break these conventions, your code is harder for others to read.

If you want to get an idea of what classes come 'pre-built' for you, just check out the API. Here is the api for java 6. You should see an "All Classes" pane. Everything in there is a class (or an interface, but don't worry about that yet). You can see there is a lot already done for you. Part of becoming a java programmer is learning enough of these that you don't re-invent the wheel.


That clarifies..!! Thank you so much. Will see you guys around a lot since i might come up with lot such doubts. Thanks, again.

Best,
Gokul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Output analysis
 
Similar Threads
Help with loop logic
Be The JVM (Head First)
unexpected results with a while counter.... (Head First, pg. 13)
Getting error please see and help it...
Java Code Error