How String Objects Are Different From Other Objects
Jan 23, 2015
how String objects are different from other objects
part 1:
// creating two objects
Dog mydog1 = new Dog();
Dog mydog2 = new Dog();
// comparing the reference variables
if( mydog1 == mydog2){
System.out.println(" The reference variables refer the same object ");
}
else {
System.out.println(" They refer to different objects ");
}
The above code works as I understand objects , it prints "They refer to different objects " to the screen.
Part - 2
// creating two objects ( I beleive, pls correct me if i am wrong )
String a = "haai";
String b = "haai";
if( a == b){
System.out.println(" Reference variables refer to same object");
When i run the above code it prints that a and b refer same object , I don't understand how they refer to same object when i didn't assign " String b = a; ". My question is did java just create one object and stored the same reference values to a and b .
I am currently working on a project where I need to return data from a database over RMI to a client who requests it. Some of the fields in the Data Object can not be seen by the client so I need to create another object to send over the network instead. The method I use is this...
public static SerializableObject createSerializableObjectFromDataObject(DataObject dataObject){ SerializableObject serializableObject = new SerializableObject(); serializableObject.setField(dataObject.getField()); serializableObject.setAnotherField(dataObject.getAnotherField()); return serializableObject; }
Is there a better way of doing this? I am creating many subclasses DataObject which all require this static method to be implemented and I can't push it into the superclass because each one needs custom behaviour.
I am little confused about String creation in java.
Doubt 1: How String objects assigned to Pool area:
1. String s="in pool"; 2. String s1= new String("not in pool");
How many objects created in statement 1 and 2. According to recent discussion with my colleague, one object created in String pool in case 1. And in case 2, two objects are created, one as literal goes to String pool and other with new() opr goes to Heap.
If above is correct, Ain't we wasting double memory for same object ? Really need clear understanding on this
Doubt 2: How does intern() work: Please see if my below explanation is correct
1. If String literal is already present in String pool , and i create a same string with new operator, reference to object is changed to pool area.
2. If String object is created using new operator and intern is called on it. If same string object is not present in the String Pool, Its moved to String pool and reference to this in Pool is returned.
I have application written in rest with Jersey-Jackson for JSON processing. All the resources produce and consume JSON. Now, the problem is, it is a server intensive application and large number of request will be hitting the server with large JSON request object. Now, because of this reason, when the JSON object gets converted in to Java object with String fields in it mapping to JSON request values large number of string objects are getting created which is resulting in frequent GC.
I want to clarify it whether this below code, when running this loop, will it create separate string objects as strings are immutable or else, will it keep the same reference(as each time inside loop, we use the same name 'rslt') and assign new string value for this?
I have a school assignment that involves me sorting an array of objects based on one of the class String variables. I am using as the title says a simple selection sort method. The problem I'm having is that when I run the program in debug mode, it never seems to enter the if statement in the inner loop. I would like to say I've tried a number of things to figure it out, but honestly I'm just stumped as to why it's not working.
Here is the code:
public static void sortTransactions(Transaction[] oTransaction){// This is the sorting method, obviously it's not done so it currently just prints to screen. System.out.println("Successful call to sortTransaction()"); String min = ""; int curInd = 0; Transaction[] temp = new Transaction[1];
[Code] ....
The output when I check to see if the array is sorted verifies that the array never does get sorted.
The thing is if SITA is the name then I have to insert component to the particular envi tabs , if DEVB is the component I have to insert the component there. How can I do this I have done some code its inserting the data at the bottom og the XML .
I'm quite new to Java. I have some trouble with understanding how to get two classes to get objects from each other (if that is the correct term).
Lets say I have a login class, in which I have a method checking what the user has entered into the console (I have not displayed the whole code, but this class works as it should and give the user an option to enter username and password and return it true if entered correct).
public static boolean validateUserPass(String userName, String password) { String[] user = {"admin"}; String[] passwords = {"firkanten"}; boolean check = false; for (int i = 0; i < user.length; i++) { if (userName.equals(user[i])) { if (password.equals(passwords[i])) { check = true;
Now, in another class I want a display box to appear on the screen and give the user three options: Click yes, no or cancel. I get this to run perfectly alone, this is not the hard part for me. I want the display box only to appear when the correct username and password is given, but I can't seem to figure out how to do this probably.
In my project I had to create 2 classes, Room and Animal.
Room had to have an array (NOT arrayList-I know theyre better and easier but I need to use an array, for now) This array must be able to be populated by 10 "Animals", and the Room class needs two methods, a method to add animal a to the room (rooms array) and a toString() that returns the name of the room and the names of the animals in the room. The teacher added a hint saying that this toString() should reference the animal classes toString()
Here is my code thus far:
public class Room { private String name; Animal[] inRoom = new Animal[10]; public Room(String roomName){ name = roomName; } public void addAnimal(Animal a){ for(int i = 0; i < 10; i++){
The thing is if SITA is the name then I have to insert component to the particular envi tabs , if DEVB is the component I have to insert the component there. How can I do this I have done some code its inserting the data at the bottom og the XML.
I am making a game in java and for the game board i want to fill the screen with blocks. to do this i stored objects of a class that displays squares into an array list and displayed the array list. however, when i do this all of the squares are drawn on top of anther at the final squares coordinates and i dont know why.
here is the code
// code for adding the squares into the array nt x = 0; int y = 0; int size = 10; static ArrayList<Map> map = new ArrayList<Map>(); //static ArrayList<Items> items = new ArrayList<Items>();
I'm having trouble creating two new die objects for the PairofDie class. I'm trying to run two separate die and print the face value and then add both numbers up and print those values as well.
public class Die { private final int MAX = 6; private int faceValue; //private int faceValue2; public Die(){ faceValue = 1;
[Code] .....
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method Die() is undefined for the type PairofDice The method Die() is undefined for the type PairofDice at PairofDice.main(PairofDice.java:6)
I would like to know how I can iterate through objects . I have a manually created linked list (without using the built-in method one). So in the memory my object looks like this(attachment).I would like to do a for loop or while loop to get each element under the test3.head.
Exercise the Coin class using a driver class in which 5 coins are flipped, as a group, 1024 times. Display the number of times exactly 4 tails are obtained. Display the number of times exactly 5 tails are obtained. Use an array of Coin.
how to put an object into an array?I know how to make a coin class with a Heads and Tails, but I dont know how to make 5 coins all at once.
public class Item{ private int myId; private int myInv; public Item(int id, int inv){ myId = id; myInv = inv;
[Code] .....
I do not understand how to complete the compareTo() method and the equals() method. I understand that the compareTo() to should return 1 if the argument is smaller -1 if its bigger and 0 if they are the same, but I do not understand how it's possible to compare the current object with the object passed in as the parameter.
This is what I tried for the compareTo():
public int compareTo(Item other){ return this.compareTo(other); }
But my compiler tells me its a recursive call so I am not sure if I am doing this correctly.
Basically I am supposed to compare an object to another but I am not sure how to access the object that is not passed in as a parameter and compare it. Same goes for the equals(). This is what I tried for that (compiler also says its a recursive call):
public boolean equals(Item other){ return this.equals(other); }
I can't come up with a simple scenario so I'll use my actual code to explain the problem, and it's going to be related to java.awt.Color, but my problem is general, not awt dependent. Although I will shorten my code to show only what I think is relevant to the problem.
Java Code:
public class GameColor { public abstract class Effect { public int strength; public Effect(int strength) { this.strength = strength; } public abstract Color get(Color color);
[Code] ....
I have my reasons for having a SetHue inner class and a setHue method that practically do the same thing. The inner abstract class Effect and inner class SetHue was static but I changed it when I testing what could cause this problem, later I will need them to be static, but since they don't affect the problem I don't think it matters if they're static or not.
So down to the problem, I create three new GameColor instances which all are 255, 0, 0, basically red. Then I do setHue(0) method on the first one, setHue(20) on the second one, and setHue(40) on the last one. And for some reason all of them have a hue of 40, whereas the last one should've had this, so basically the last time I call setHue on the instances of GameColor, changes the hue of all of these colours to that hue. I want it to be individually affected, but all of them are affected. This is where I apply these changes:
I am currently trying to implement a hashmap from a class i currently have that is called 'Paper'
My paper class consists of:
String Title; String[] author;
What I am wondering, is if there is anyway I can call these seperate attributes to be both the key and the value of the hashmap. Currently I cannot see a way to call individual values, only the class itself.
I have a ArrayList of objects of class called HockeyPlayer (ArrayList<HockeyPlayer>). A HockeyPlayer has a String name and int number of goals.
This is my current work for comparing each object in the list to every other and printing them to screen:
Iterator<HockeyPlayer> it = hockeyPlayersList.iterator(); while (it.hasNext()) { HockeyPlayer singleHockeyPlayer = it.next(); //the first one encountered // HockeyPlayer nextHockeyPlayer = it.next(); //the next one encountered
[Code]...
This of course produces some duplication in the print-out: These are the equal hockey players: Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
These are the equal hockey players: Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
I have GameConsole class with gamePlay(). There are two objects in this class that I want to access from a class in another package.
package blackjackControls; public class GameConsole { Player dealer; //both of these objects I am trying to bring over into the blackjackGUI package Deck playingDeck; public void gamePlay(){
[code].....
The dealer and playingDeck objects are giving me an error of unresolved. the way it is written I also get a static error on line 37. I know that I have not yet written in the actionEvent statement in the button constructor.
The System.out.println("jsp page: .... &> results in the output: "jsp page: movielist - [Title: Die Hard; Budget: 20000000, Title: two days in paris; Budget: 1000000]" so I am confident the objects are being loaded into the ModelAndView correctly. However the output of the block is "${movie.name}" instead of the list of movies. My movie object has a getName() method to return a string (and a setName() method). I am not sure why the System.out.println statement can find the movielist attribute, but ${movie.name} is being treated a plain text. There are no execptions thrown or other indications of errors.
I want to clone some Arraylist, but the compiler apparently are just referencing the values to it's original ArrayList. I don't know what should it be:
package projetoteste; import java.util.ArrayList; import java.util.List; public class TestFood { public static void main(String[] args) { List food=new ArrayList();
[code]....
Notice that ArrayList dailyMeal should be untouchable, but it return the changes that I made in local for-loop iteration although I didn't added nothing to it, just to it's clone.
public class MyExample extends GCompound { //instance variables public GRect R1 = new GRect(0, 0, 20, 20); public GRect R2 = new GRect(0, 0, 5, 5); //R2 is in front of R1, but its coordinates are all "inside" of R1's coordinates //constructor public MyExample() { add(R1); add(R2); } }
1) Suppose I'm in a GraphicsProgram and declare:
MyExample myex = new MyExample();
Suppose I have coordinates (x1, y1), and want to find out whether this is the myex object defined in the previous line. I would use:
obj = getElementAt(x, y); if (obj == myex) (...and so on)
Now suppose I want to test whether the object is the GRect object R1 within myex. Why doesn't the following work?
if(obj ==myex.R1) (...and so on);
Here is the full code that shows my question; it outputs "myex", none of the other outputs come out...