How Do Memory Allocate Space For Objects Or Variables
Sep 1, 2014
I'm new to programming and I would just like to know what happens inside the memory once you run a java application. How do the memory allocate space for your objects or variables? Where does it start?does it start in memory location 1 first when I create my first variable or object? What is the use of variables and how is it used by the memory?Also if I create a variable named String s1 then I created another variable again named s1= "myString" Did I create a new object or I just edited the value of the s1 variable??
And if I create a variable int num1 = 10 then I created another variable int num1 = 12 did I create another one or I just edited the num1 variable?? Also how much memory would an object have? Does it depend on how many primitives objects like arrays or strings it have?Also I want to develop 3D games also, in the future is Java going to be great as I know machines in the future will have massive amounts of RAM and the JVM will be better...
I was wondering where is the memory allocated for an applet; by the browser; by the JVM; some applet specific java option? I get an out of memory error when running my applet (loading pictures).
I have a simple JavaFX Application that open a Browser and shows google page. After exit the Application and free all objects, I can see that the JavaFX objects like Scene, Stage, WebView and WebEngine are alive in the heap memory after call GC. I can see this objects with JProfiler and other Profiler tools.
This is my Test code: import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javafx.application.Application; import javafx.application.Platform;
To test the application click on Start Button to show google web page, click on Stop Button to stop the application, run a Profiler tool, and call gc, the JavaFX classes are alive. I am using java version "1.7.0_51" and windows 8.1 Is there something wrong in my code? Or this is the normal behavior?
Let's say I have a loop that loops through objects in an ArrayList then does stuff with them. Is it better for me to store the object in a temporary local variable and do stuff with it, or always use the ".get(arrayindex)" thing?
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.
As web server has multiple threads to serve client requests in Thread Pool & to ensure Thread Safety we should not use any variables or Objects at Instance/Class level.But in case of Session Variable which one is the Best Practice as the Session object is used by all the requests to have the same Session ID.
My Code :
public class MyServlet extends HttpServlet { private static Logger log = Logger.getLogger(ClientRegistrationServlet.class); private HttpSession session; /* This is used at Instance Level*/ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
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.
In c++, I'm aware that you can use the ignore function to ignore numbers after space, but in string how do I ignore a number after space is found for instance, "109 33" how would I ignore 33?
I have an assessment for college, the part of my code that im struggling with is the part where the user enter their first name followed by a space and then their second name. I m using ---------------fullname = JOptionPane.showInputDialog("enter your firstname followed by a space then your second name " ); ------------- to capture this. the issue is if the user only enter 1 name I need to output an error. the issue is I cant find a way to tell if the user entered a second name. This is what I have so far:
public void makename() { // makes an inputbox to ask their name fullname = JOptionPane.showInputDialog("enter your firstname followed by a space then your second name " ); //separates the first and second name into 2 strings in order to make the username
[code]....
The problem is it accepts anything I type in without causing an error. What should I type in to stop this? ive tried anything but I cant find a way to tell if a surname has been entered or not.
eg.ubccdddwfreshawbgtiijhktrocbfgrtwghdddguppgrkitt. etc.
inputted from the keyboard one character at a time starting with a vowel (The vowels are a, e, i, o, and u) until a blank {spacebar} character is issued. You do not press enter after each character. Any other sequence should return an error message and a way of entering it correctly.
Having read in the list (or during the reading of the list) the program is to find the longest continuous occurrence of consonants, outputting the vowels between which this occurs and the length of this string of consonants.
{In the example above, the output would be 13ou}
Check for input errors and respond accordingly.
I have done most of it, the problem being that I don't know how to end the scan with a space so that I don't have to press enter. I have done the bit that finds the biggest amount of consonants in a row. (I'm writing in netbeans)
I have been trying to space out output on a Java console window so that I have three columns with 6 rows of data from three different arrays. The code I have so far outputs the data with no problem however the spacing between the columns is uneven. My loop so far is made up as follows
for (int i = 0; i < printVotes.length; i++) { System.out.println(printNames[i] + " " + printVotes[i] + " " + printPrecent[i] + "%"); }
As you can see I have been manually adding the space between each element of the array but this means that the space between each element is different because the size of each element is different if work out a loop that works out an even amount of space between the elements and then print this along with the elements ....
I want to cut my string from space char but i am getting exception....
Java Code:
import java.util.Scanner; import java.util.StringTokenizer; public class NameSurname { public static void main(String args[]) { Scanner sc=new Scanner(System.in); String s0,s1=null,s2 = null,s3=null; s0=sc.next();
[Code] ....
Console: Lionel andres messi Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at java.util.StringTokenizer.nextToken(Unknown Source) at com.parikshak.NameSurname.main(NameSurname.java:15) mh_sh_highlight_all('java'); I/p -O/p:
my s0=Lionel andres Messi
And I want to break it as soon as i find space and save it in s1,s2 and s3
I am working on my homework, and everything is fine. But I was wondering if I can fix one thing. basically it prompt from a user for a text file name and save the content as an array. some part of my code is here:
public class arraysort{ static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception { System.out.print("Enter the first name of the file: "); String input = kb.readLine(); int[] firstArray = readFile(input); firstArray = bubbleSort(firstArray);
As you can see instead of displaying the champion name it is displaying the memory location and I do not know how to fix it.
class Champions { String name; Champions [] weak = new Champions [3]; Champions [] strong = new Champions [3]; String [] items = new String [3]; public static void main (String [] args) {
For some reason my code returns the memory address of the array when its a print statement with a string, but it works fine when its in a separate print statement all by itself. Why? Do I need to create a toString method that converts a char array to a String to something? The reason why I ask that is becuase on Eclipse line 10 has a warning stating "Must explicitly convert char[] to a String".
public class Ex { private String word; public Ex(String word) { this.word = word; } public char[] Display(){ char[] wordChars = this.word.toCharArray(); return wordChars;
[Code] .....
Result:
Hello world The word is: [C@1db9742
I also tried this, knowing that it's a long shot, but that didnt do anything...
public String toString(){ Ex ex = new Ex(this.word); char[] word = ex.Display(); String updated = word.toString();//counter intuitive? return updated; }
So I have that traditional memory game homework assignment and for some reason I can't figure out how to put the codes in order. I can't figure out the arrangement to make it work. So far I have
import java.util.Random; import java.util.Scanner; public class MemoryGame {