Java Programming Code

Recommended Posts

I am new to programming and I do not know what output this code makes. I tried to compile it, but there seem to be errors on the apostrophes. Please explain why there are apostrophes on the first two and not the last one... Thanks!

public class FirstProgram

{

public static void main(String args[])

{

char a, b;

a = 'b';

System.out.println(a);

b = 'c';

System.out.println(b);

a = b;

System.out.println(a);

}

}

Share on other sites

I don't know Java, but I'd expect the intent is:

1. Put character 'b' into variable named a.

2. Put character 'c' into variable named b.

3. Put whatever is in variable b, into variable named a. (That's the diff with not having the quotes).

So the three prints I'd expect to show:

b

c

c

Dunno why it won't compile for you. (What exactly is the compiler telling you?)

e.g. (for comparison) this works fine in C#:

using System;

namespace SimpleJavaEquiv
{
class Program
{
static void Main(string[] args)
{
char a, b;
a = 'b'; // char literal into a
Console.WriteLine(a);
b = 'c'; // char literal into b
Console.WriteLine(b);
a = b; // copy b (which is 'c') into a (overwriting its 'b')
Console.WriteLine(a);

// Just to pause to see result
}
}
}

Edited by pzkpfw

Share on other sites

I don't know Java, but I'd expect the intent is:

1. Put character 'b' into variable named a.

2. Put character 'c' into variable named b.

3. Put whatever is in variable b, into variable named a. (That's the diff with not having the quotes).

So the three prints I'd expect to show:

b

c

c

Dunno why it won't compile for you. (What exactly is the compiler telling you?)

e.g. (for comparison) this works fine in C#:

using System;

namespace SimpleJavaEquiv
{
class Program
{
static void Main(string[] args)
{
char a, b;
a = 'b'; // char literal into a
Console.WriteLine(a);
b = 'c'; // char literal into b
Console.WriteLine(b);
a = b; // copy b (which is 'c') into a (overwriting its 'b')
Console.WriteLine(a);

// Just to pause to see result
}
}
}


The complier is telling me that the apostrophes are "illegal characters." Thank you for your detailed explanation, but I thought that the second one, "b = 'c'" should not work because c has not been declared in the beginning. Or does it not matter?

Share on other sites

The 'c' char literal doesn't need to be declared, it's a literal. The language knows what the letter 'c' is. (It's (more or less) 99 !)

The a and b char variables do need to be declared. That's a difference between a literal and a variable.

The "char a, b" is saying "I'm going to have two places to put characters, the places will be named a and b. The characters that are put into those places can be whatever characters the language knows about.

So:

b = 'c' : is putting the letter 'c' (which the language already knows about) into a place called a (which you declared above).

But:

a = b : is putting the content of the place called b into the place called a.

Having had a quick google on Java syntax, I'm perplexed by the error message you are seeing. Is what you posted in post #1 exactly what you have in your code?

P.S. googling for: java character literal I see: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

... which does seem to show apostrophes are legal syntax.

Edited by pzkpfw

Share on other sites

It's not a language I know but if this example
http://en.wikipedia.org/wiki/Java_(programming_language)#Hello_world
is right then you need to use quote marks " rather than apostrophes ' to denote string litterals

Share on other sites

According to what I've found, a character literal in Java is written with the apostrophes, strings with quotes. In your example, that's a string being printed. In post #1, a and b are declared as char. The link in post #4 has examples of both string and character assignment.

Edited by pzkpfw

Share on other sites

Ohh, wow. That explains it, but what am I supposed to replace the string with? I'm sorry for asking so many questions, but I'm completely new to this...

But thanks to everyone that helped! I wasn't expecting so many replies

Share on other sites

It's not a language I know but if this example

http://en.wikipedia.org/wiki/Java_(programming_language)#Hello_world

is right then you need to use quote marks " rather than apostrophes ' to denote string litterals

character is single letter f.e. 'x'

string is class, internal array of dynamically allocated chars, and you initialize it by f.e. "test".

http://docs.oracle.com/javase/tutorial/java/data/characters.html

public static void main(String args[])

Shouldn't here be

public static void main(String[] args)

?

Try also this, whether it compiles fine:

public class FirstProgram
{
public static void main(String[] args)
{
char a, b;
a = 'b';
b = 'c';
a = b;
}
}
Remember that there are two apostrophes keys that looks very similar.. !
One you have below ESC key, on right of 1 digit key.
Second one you have on left of ENTER key.
You should use proper one.
Edited by Sensei

Share on other sites

(re: post #7, Post#6 was more in response to post #5. For some reason certain features like Quote don't work for me in IE, and I can't always be bothered running up firefox.)

Share on other sites

Copy/pasted the code from post #1 into it - and it ran first time, giving the expected result.

Share on other sites

When we will replace 'b' by b (secondary apostrophes)

Compileonline.com is showing following error:

"Compiling the source code....

\$javac FirstProgram.java 2>&1

FirstProgram.java:7: error: illegal character: \96

a = b;

^

FirstProgram.java:7: error: illegal character: \96

a = b;

^

FirstProgram.java:7: error: not a statement

a = b;

^

3 errors

"

Is it what you see.. ?

Edited by Sensei

Create an account

Register a new account