Why r we allowed to create String objects with & without using new operator
While creating any object, usage of the new operator is a must...but only for the string object we can create it with and also without new operator how is it possible to create an object without using new operator.
Because Mr. (Dr.?) Gosling is a kindly soul who realizes that programmers have deadlines and, when he designed Java, was not so rigid or unbending as not to realize that from time to time, certain shortcuts are warranted, even in a relatively pure language such as Java. The direct String literal assignments are a shortcut over using the new operator making Java programming (and execution; there's also a performance benefit) more streamlined.
So look not the gift horse in the mouth, but simply bask in the simplification and ease on the eyes and directly assign your little literals to your heart's content.
Similar Messages
-
Create a line with arcs using new SdoApi (not liniear)
Hi
How we create a line with arcs using sdoapi (not liniear line)?
PhilippPhilipp,
You have to use the default constructor for JGeometry
to build lines with arcs:
public JGeometry(int gtype,
int srid, double x, double y, double z,
int[] elemInfo, double[] ordinates)
Currently, this is the only way to create lines with arcs.
siva -
String s= new String("abc");
String s1="abc";Is there any performence issue between these two?
I think both will take same time to create objects.Yes, there is a performance implication.
When using the first line String s = new String("abc"); you are actually creating two objects. As a general rule, you should never use a String class constructor when initializing to a literal value. Even doing String s = new String("abc" + someVariable) is creating one more object than needed. "abc" is the firs object, the result of "abc" + someVariable is the second object, and the String created by the constructor is the third.
It should be noted most recent JVMs use some level of caching of String Literal value objects (I believe this goes as far back as JDK 1.3), such that "abc" is only be created once by the JVM (though I'm not sure if it will cache all literal String objects or use some type of LRU algorthm to discard some of them). Regardless, there is some level of efficiency you get from the JVM doing this. Thus if a method containing String s = "abc" is called repeatedly, a reference to the same String object should be used everytime; there will be no further creations of the object as long as the JVM keeps it in cache.
Using String s= new String("abc") will leverage the cached literal reference to "abc", but also creates a new instance of String to refer to the literal everytime your method is called. This is much less efficient.
Run the following code (I did it using JDK 6):
package org.einsteinium.samples;
public class StringTest {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
boolean equalMethodTest = s1.equals(s2);
System.out.println("Are the objects the same using the equals method? " + equalMethodTest);
boolean equalOperatorTest = (s1 == s2);
System.out.println("Are the objects the same using the equal operator? " + equalOperatorTest);
}It will print:
Are the objects the same using the equals method? true
Are the objects the same using the equal operator? trueNow if you re-write it to:
package org.einsteinium.samples;
public class StringTest {
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = "abc";
boolean equalMethodTest = s1.equals(s2);
System.out.println("Are the objects the same using the equals method? " + equalMethodTest);
boolean equalOperatorTest = (s1 == s2);
System.out.println("Are the objects the same using the equal operator? " + equalOperatorTest);
}it will print:
Are the objects the same using the equals method? true
Are the objects the same using the equal operator? falseI hope this helps some. -
Why not use "new" operator with strings
why we not use new when declaring a String .because in java String are treated as objects. we use new operator for creating an object in java .
and same problem wiht array when we declare array as well as initialize .here we are alse not using new for array
whyStrings aren't just treated as objects, Strings are Objects.
As for why not using new for Strings, you can, if you want.:
String str = "this is a string";
and
String str = new String("this is a string");
do the same thing (nitty-gritty low level details about literals aside). Use whatever you like, but isn't it simpler not to type new String(...) since you still need to type the actual string?
As for arrays, you can use new:
int[] ints = new int[10];
or
int[] ints = { 0, 1, 2, 3, ..., 9 };
But the difference here is you are creating an empty array in the first one, the second creates and fills the array with the specified values. But which to you often depends on what you are doing. -
How to create a viewobject dynamically without using wizard
Hi,
I am Using jDEV 11G, i need to create a viewobject dynamically without using wizard, without binding from any entity.
Actually my intention is to make a grid like in .Net, when a user want to create a new row in RichTable without using DB.
just like shopping cart.
i have done thsi code:
ViewObjectImpl view=new ViewObjectImpl();
view.addDynamicAttributeWithType("Att1","String",null);
view.addDynamicAttributeWithType("Att2","String",null);
view.addDynamicAttributeWithType("Att2","String",null);
Row rw=view.createRow();
rw.setAttribute("Att1","First1");
rw.setAttribute("Att2","First2");
rw.setAttribute("Att2","First3");
view.insertRow(rw);
I have a RichTable , i need bind this viewobject into that.
Edited by: vipin k raghav on Mar 10, 2009 11:39 PMHi Vipin,
You can create the view object with rows populated at run time.
[http://download.oracle.com/docs/cd/E12839_01/web.1111/b31974/bcquerying.htm#CEGCGFCA]
For reference of how to create an empty rwo at run time on button click
[http://kohlivikram.blogspot.com/2008/10/add-new-row-in-adf-table-on-button.html]
~Vikram -
How to create inspection lot manually without using QA01 Transaction code?
Hi,
Please anybody give procedure for creating Inspection Lot Manually without using Transaction Code QA01Hello, Srinivas,
If you want another T code only then you can try QA01A.
or you can create a Physical Sample with QPR1 and then click on the button there to create inspection lot. To create inspection lot for that Physical sample (you will able to see that button only after releasing the sample) Or you can use QPR5.
But let me know the reason why you want T code other than QA01.
Regards,
Shyamal -
CREATE XML PUBLISHER REPORT WITHOUT USING RDF
Hi Every one ,
Can anybody share his/her expertise on following issue ?
"How to create XML Publisher Report without using RDF?"
Your help is appreciated.Hi Swapnesh
YOu do not need to create the XDODTEXE, its seeded for you. You need to do the following:
1. Create and test the data template
2. Load the data template to the template manager against a data definition
3. Create a concurrent program definition that uses the XDODTEXE as its executable
4. Create parameters (if required) for the CP and DT that match in name
You're done
Regards
Tim -
How can i create an apple id without using a visa or credit card
how can i create an apple id without using a visa or credit card
Follow the directions here, EXACTLY:
http://support.apple.com/kb/ht2534 -
Error while creating sales order with ref using SD_SALESDOCUMENT_CREATE
Hi
I am creating sales order with reference using function module SD_SALESDOCUMENT_CREATE , while creating i am getting error
Document doesn't have document category even thougth i am passing document category in header as well as item lavel.
In VBUK table there is no value for VGTYP.
Can you please help me how to resolve it.
Thanks & regards
kiranProblem resolved by passing REFDOC_CAT in the FM along with SD_DOC_CAT.
Thanks -
I am creating a slideshow with elements using photos and videos but the video sound doesnt play?
Hello jive
I am playing it back in the slideshow editor/setting up part of
programme where you determine photos/video to be included gaps between
slides, music to play etc This goes through each phot and when comes
to a video the background sound doesnt play, I am using windows 7 and
elements 10.
regards gins51 -
How to create an apple id without using credit card
how to create an apple id without using credit card.. i have recently purchased apple iphone 3gs bt unable to get apps because of giving credit card info which i dnt hav.. so plz tel me some way to process..
To get the none option, sign out of anything apple you're signed into. Go to the app store and try to download a free app, any free app. Instead of signing in, take the option to to make a new apple ID. Then go through the steps to make the ID and you'll see the 'none' option.
-
HT2534 i am not getting "none" option while creating an apple id without using credit card.
i am not getting "none" option while creating an apple id without using credit card.
To get the none option, sign out of anything apple you're signed into. Go to the app store and try to download a free app, any free app. Instead of signing in, take the option to to make a new apple ID. Then go through the steps to make the ID and you'll see the 'none' option.
-
Why i can not hear directly from my iphone without using hand free?
why i can not hear directly from my iphone without using hand free?
Hello oshoke21,
The following article provides steps that can help get your iPhone's speaker working.
iPhone: No sound or distorted sound from speaker
http://support.apple.com/kb/TS5180
Cheers,
Allen -
Create an object with the name passed in as a string argument to a method
Hi,
I am sorry if it's too trivial for this forum , but I am stuck with the following requirements.
I have a method
void abc(String object_name, String key, String value)
method abc should first check if there exists in memory a hashmap object with the name passed in as argument object_name.
If yes ..just put the key and value there.
if not , then create the hashmap object called <object_name> and insert the key/value there.
Can anybody help me in the first step i.e, how to check if an object exists with the name passed in and if not then create it.
Will getInstance method be of any help?
Thanks in advance for your response.
-Sub-javaDear Cotton.m,
Thanks for your suggesstion. I will remember that.
But somehow I have a strong belief that you still need to consult dictionary for exact meaning of the words like "upset" , "frustration" etc. Not knowing something in a language , that too as a beginner, does not yield frustration, but increases curiosity. And people like petes1234 are there to diminish that appetite.
To clarify above, let me compare jverd's reply to my doubt with petes1234's.
jverd said it cannot be done and suggested a work around (It was perfect and worked for me) While petes1234 , having no work in hand probably, started analysis of newbies mistakes.
jverd solved my problem by saying that it cannot be done. petes1234 acted as a worthless critic in my opinion and now joined cotton.m.
Finally, this is a java forum and I do not want to discuss human characteristics here for sure.
My apologies if I had a wrong concept or if I chose a wrong forum to ask, where people like petes1234 or Cotton.m show their geekdom by pointing out "shortfalls" rather than clearing that by perfect examples and words.
Again take it easy and Cotton.m , please do not use this forum to figure out others' frustration but be a little more focussed on solving others "Java related" problems :)
-Sub-java -
How to create C201 object with BAPI in ECC6.0
BAPI_ROUTING_CREATE can not create C201 object.Because its plnty is always set to 'N'.In C201, the plnty should be '2'
Write a method
public void MsgBox(String ex, Page pg,Object obj)
string s = "<SCRIPT language='javascript'>alert('" + ex.Replace("\r\n", "\\n").Replace("'", "") + "'); </SCRIPT>";
Type cstype = obj.GetType();
ClientScriptManager cs = pg.ClientScript;
cs.RegisterClientScriptBlock(cstype, s, s.ToString());
and when you would like to use messagebox just use this line
MsgBox("your message", this.Page, this);
Maybe you are looking for
-
Can I use my Nike+ with 2 Different Computers: my home mac and work PC?
I use my Nike+ with my Nano on my home iMac and track my runs on Nike+.com. Works great. However, I travel for work and will be back on the road next week with my work PC. I don't have itunes loaded on my work PC (not allowed - its not "corporate" so
-
How to create a unique group field in Qualifier table
Hi, I am trying to use a qualified table in Product maintable like "Site items". However I would like to have two non-qual field in that like 'Plant' and 'item#' which should be an unique combination. I have tried to create a table(X) with the above
-
CS5 InDesign cannot open files in the InDesign CS5 document format
I'll be working in InDesign CS5 with no problems then at some point I'll double click on an InDesign file and a window comes up and says - InDesign cannot open files in the InDesign CS5 document format. Sometimes when this happens I have to completel
-
i don't know how update or download the app: MAC APP STORE. i have the app but every time i open, that closes. Plues the icon is a blank sheet of paper whit two pencils.. help!!
-
XY graph cursor legend in another VI
Hi, I have a XY graph on my main VI. On the main VI there is little space for the cursor legend so I would like to move it into a separate frontpanel that can be popped up on demand. I managed to use the graphs reference to get all information about