Jump to content

why java occur error

buklu

public static void main( String[] args ) {
int j = 2;
String s = "";
while ( j <= 8 ) {
s += "*";
j += 2;
}
System.out.println( j + " " + s );
}
without String s = ""; there will be a error

In this case  ,System.out.println( j + " " + s );

s equal " ",Isn't " " duplicate?

Link to comment
Share on other sites

Link to post
Share on other sites

Waaah! Please use the code tag please: https://linustechtips.com/main/announcement/12-please-use-code-tags/

But what do you mean..? Without declaring the String variable "s", you can access add anything to it? Makes sense to me.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, buklu said:

If i delete String s = "";   there is an error

Yeah, as I said you need to declare a variable before you can use it (in Java).

e.g. if I do this:

//if I do this, it wont work
someVar = 1 + anotherVar;

//this will work, because I declared the variables that are used
int someVar = 0;
int anotherVar = 4;
someVar = 1 + anotherVar;

 

Are you perhaps moving from another language to Java? Some languages allow you to just use variables without declaring them (I know PHP can do that, pretty sure Python can as well), but in Java you need to first declare a variable (say what kind of variable it will be) before you can use it.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, buklu said:

If i delete String s = "";   there is an error

well yes because you're asking for a variable that doesn't exist.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, Minibois said:

Yeah, as I said you need to declare a variable before you can use it (in Java).

e.g. if I do this:


//if I do this, it wont work
someVar = 1 + anotherVar;

//this will work, because I declared the variables that are used
int someVar = 0;
int anotherVar = 4;
someVar = 1 + anotherVar;

 

Are you perhaps moving from another language to Java? Some languages allow you to just use variables without declaring them (I know PHP can do that, pretty sure Python can as well), but in Java you need to first declare a variable (say what kind of variable it will be) before you can use it.

what is the function of String s = ""

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, buklu said:

what is the function of String s = ""

Declaring the variable which you call 's' as a String variable.

Declaring = telling the programming language to reserve a spot in the system memory for a String variable (a String variable is a piece of characters, like letters, numbers, special characters.. etc. What we normal humans would call "a (piece of a) sentence". This piece of memory can be called by its name ("s" in your case) later in the program.

 

You say "= "";" to give variable 's' a value. Because you can't add something to nothing. The variable is empty when you declare it just like "String s;" so you can't add things like "s += "hello"" without first giving it a value. The empty quotes is just nothing that you add to it.. But it's part of declaring a variable; giving it a value.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Minibois said:

Declaring the variable which you call 's' as a String variable.

Declaring = telling the programming language to reserve a spot in the system memory for a String variable (a String variable is a piece of characters, like letters, numbers, special characters.. etc. What we normal humans would call "a (piece of a) sentence". This piece of memory can be called by its name ("s" in your case) later in the program.

 

You say "= "";" to give variable 's' a value. Because you can't add something to nothing. The variable is empty when you declare it just like "String s;" so you can't add things like "s += "hello"" without first giving it a value. The empty quotes is just nothing that you add to it.. But it's part of declaring a variable; giving it a value.

huge thanks,what a patient dude

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, buklu said:

huge thanks,what a patient dude

By the way if I wasn't clear on the "declaring the variable with a value" look up 'null' in programming context.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×