473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Variable Declaration

Guys,

Please help me by answering my questions. Thanks in advance

1. What is the difference between below two declaration statements

int i; i=20;
and
int i=new int(); i=20;
2. String is a class. But why its not declared using "new" keyword?

-Vasanth TT

May 16 '07 #1
6 1621
On May 16, 9:07 am, Vasanth TT <vasanth...@gmail.comwrote:
1. What is the difference between below two declaration statements

int i; i=20;
and
int i=new int(); i=20;
They are both the same, although its odd to see the second form.
2. String is a class. But why its not declared using "new" keyword?
It can be:
string x = new string();

The compiler is smart enough to change:
string x = "My string";
to
string x = new string( "My string" );

May 16 '07 #2
On May 16, 2:22 pm, Andy <a...@med-associates.comwrote:
On May 16, 9:07 am, Vasanth TT <vasanth...@gmail.comwrote:
1. What is the difference between below two declaration statements
int i; i=20;
and
int i=new int(); i=20;

They are both the same, although its odd to see the second form.
Indeed - basically the first value is ignored.
2. String is a class. But why its not declared using "new" keyword?

It can be:
string x = new string();
Except that string doesn't have a parameterless constructor ;)
The compiler is smart enough to change:
string x = "My string";
to
string x = new string( "My string" );
Not quite - new always (or at least should always) return a new
object, whereas string literals are interned. So if you do:

string x = "hello";
string y = "hello";

then x and y both refer to the same object, which they wouldn't if the
"new" operator were used.

Jon

May 16 '07 #3
On May 16, 6:52 pm, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On May 16, 2:22 pm, Andy <a...@med-associates.comwrote:
On May 16, 9:07 am, Vasanth TT <vasanth...@gmail.comwrote:
1. What is the difference between below two declaration statements
int i; i=20;
and
int i=new int(); i=20;
They are both the same, although its odd to see the second form.

Indeed - basically the first value is ignored.
2. String is a class. But why its not declared using "new" keyword?
It can be:
string x = new string();

Except that string doesn't have a parameterless constructor ;)
The compiler is smart enough to change:
string x = "My string";
to
string x = new string( "My string" );

Not quite - new always (or at least should always) return a new
object, whereas string literals are interned. So if you do:

string x = "hello";
string y = "hello";

then x and y both refer to the same object, which they wouldn't if the
"new" operator were used.

Jon
Yes, Jon is right. It's just the same as in Java.

BTW it's true for all literals.

Event if I do string x = new string("hello");

It will refer to the same object.

Cheers,
Moty

May 16 '07 #4
Moty Michaely <Mo*****@gmail.comwrote:
Yes, Jon is right. It's just the same as in Java.

BTW it's true for all literals.

Event if I do string x = new string("hello");

It will refer to the same object.
Well, that won't compile in C# (there's no string constructor taking
just a string parameter), and in Java it *won't* refer to the same
object. Here's the code to prove my point for Java:

public class Test
{
public static void main (String[] args)
{
String x = new String("hello");
String y = new String("hello");
System.out.println (x==y);
}
}

Run it and it prints "false".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 16 '07 #5
What is the feedback for this answer given in this blog
http://cognitionspot.blogspot.com/
On May 16, 11:12 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Moty Michaely <Moty...@gmail.comwrote:
Yes, Jon is right. It's just the same as in Java.
BTW it's true for all literals.
Event if I do string x = new string("hello");
It will refer to the same object.

Well, that won't compile in C# (there's no string constructor taking
just a string parameter), and in Java it *won't* refer to the same
object. Here's the code to prove my point for Java:

public class Test
{
public static void main (String[] args)
{
String x = new String("hello");
String y = new String("hello");
System.out.println (x==y);
}

}

Run it and it prints "false".

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 17 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
2
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
1
NeoPa
by: NeoPa | last post by:
Problem Description : In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private;...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.