I have a question regarding best practice in using local variables as my method return variable. I have a method like this:
myReturnObject getMyObject(String input) {
myReturnObject myObject = null;
try {
myObject = helperObject.someOtherMethod().getObject(input); //getObject has return type myReturnObject
} catch (Exception e) {
//log any problems
}
return myObject;
}
And I'm wondering if I rewrite like this if I'll see some performance optimization benefit:
myReturnObject getMyObject(String input) {
try {
return helperObject.someOtherMethod().getObject(input); //getObject has return type myReturnObject
} catch (Exception e) {
//log any problems
}
return null;
}
myObject can be quite large -- so I'm wondering if I can omit the myReturnObject local variable instance if it'll save some work from the garbage collector.
The term "Local variable" is related to scope. That is a local variable is one which is defined in a certain block of code, and its scope is confined inside that block of code.And a "Member variable" is simple an instance variable.
I read in a discussion forum that when local variables are declared (example code below), their name reservation takes place in memory but they are not automatically initialized to anything. On the other hand, when member variables are declared, they are automatically initialized to null by default.
Java Code: public void myFunction () { int [] myInt; // A local, member variable (because "static" keyword is not there) declared } mh_sh_highlight_all('java');
So it seems that they are comparing local variables and member variables. While I think a member variable can also be be local in a block of code, isn't it?
I've been tasked with processing a large dataset as part of a class assignment. One of the fields is a 24-digit unsigned hex number. I realized that, rather than storing the field verbatim in a char array of length 24, I could store the actual value of the hex number in an array only 6 chars long (I chose char over int because chars are unsigned). To do this, I wrote the following simple class to accept the hex string and convert it so that it can be stored in that manner:
private class Hex24 { private final char[] hexAsInt = new char[6]; public Hex24(String h) { for(int index = 0; index < 6; ++index) hexAsInt[index] = (char)Integer.parseUnsignedInt(h.substring(index * 4, (index + 1) * 4));
[Code] ....
Assuming all this works (haven't tested it, but I think it should), what I'm wondering is how much memory this will save me (if any) compared to just throwing everything into a char[24] array. The underlying char[6] is obviously quite a bit smaller, but objects must take up more space than just their fields since Java needs to know what kind of object it is so it can know what methods it has, etc..How to accurately compare the size of a Hex24 object to the size of a 24 character array.
I guess another option/thing I might want to compare size efficiency for is putting the char[6] along with the conversion/comparison logic directly into the classes (as fields/member methods) where I'm currently using Hex24 fields. I'd guess this is the most memory-efficient option I've come up with, but it would lead to a lot of code duplication.
Another thing I'd like to compare is the size of a String vs. the size of the equivalent char array for shortish text fields. If this difference is big enough it might be worth storing those fields as arrays rather than Strings.
i have to write more than 100000 rows in a excel sheet (file size more than 20 MB) via java.
when i use XSSF, i am getting below Error.
java.lang.OutOfMemoryError: Java heap space at org.apache.xmlbeans.impl.store.Saver$TextSaver.resize(Saver.java:1592) at org.apache.xmlbeans.impl.store.Saver$TextSaver.preEmit(Saver.java:1223) at org.apache.xmlbeans.impl.store.Saver$TextSaver.emit(Saver.java:1144)
[Code]....
when i use HSSF , i am getting the below Error. java.lang.OutOfMemoryError: Java heap space
I have tried increasing the java heap size , by giving upto -Xms1500m -Xmx2048m
I am reading input from a file that has following information:
line 1 = numbers of integers in array, line 2 = elements in array1, line 3 = elements in array2.
These lines constitute a test case. There are 1000 test cases in the input file.
So basically, I read the length of arrays, populate the arrays by reading from the file.
The code is below ( I have not included reading input code):
while(test_case<1000){ if (count == 1){ //count keeps track of lines in input file vec_length = Integer.parseInt (tokenizer.nextToken()); count++; continue; } if (count == 2){ //populates array1 vector1 = new int[vec_length]; for (int i = 0; i < vector1.length; i++) vector1[i] = Integer.parseInt (tokenizer.nextToken()); count++; continue; }
Array2 is populated using the same as above code. However when I use the following code:
for (int i=0; i<vec_length; i++) temp += vector1[i]*vector2[i];
I get " local variable vector1 and vector2 have not been initialized error". But both arrays have been initialized in the if{} block. Is it because initialization was local to if block?
As a studyproject I'm currently writing a class the allows me to get al fun dates (like when eastern is in a given year, what day a given date has, calculate the date of tomorrow).
While working on the following method:
public String getNextDate (int day, int month, int year) { String nextDate; int nextDay = getNextDay(day, month, year); int nextMonth = getNextMonth (day, month, year); int nextYear = getNextYear (day, month, year); return nextDate = "the day after " + month +"-" + day + "-" + year + " is " + nextMonth + "-" + nextDay + "-" + nextYear + "."; }
I get a notion in my lovely IDE (eclipse) reminding me I'm not using nextDate ("The value of the local variable nextDate is not used"). But I feel I really do use nextDay here. So either I'm making a coding(style) mistake giving me this notion or I should just ignore this notion.
Here, I have just tried out to take a value from the database and storing it into local variable then I want to have that value in the value attribute of <input> tag but somehow, I can't get it..
Here, below is my code..
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL]...."> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
I just started using java because i want to create a simple web service that will take some values from within a url and save those values in variables.
To be more specific:
For my project I use the Spring Tool Suite.
I want to be able to enter a URL in a browser.. something like "localhost:8080/test?name=Root". When I hit Enter a page will be displayed showing "Hello Root" if the name in the URL is Root or "Hello User" for any other name.
How to force browser to open/save/save as the file from server instead of browser cache.
I am creating a csv file through a pl/sql procedure and forwarding the link to user once user clicks on link he downloads the file, however if the same thing is repeated then browser returns the old cached file instead of new file generated on server.
you can also refer this link Local variables in java?Local variables in java?To meet temporary requirements of the programmers some times..we have to create variables inside method or bock or constructor such type of variables are called as Local Variables.
----> Local variables also known as stack variables or automatic variables or temporary variables
----> Local variables will be stored inside Stack.
-----> The local variables will be created while executing the block
in which we declared it and destroyed once the block completed. Hence the scope of local variables is exactly same as the block in which we declared it.
package com.javatask.in; class A{ public static void main(String args[]){ int i=0; // Local variable
I edited some lines from "[URL] ...." and saved it as html file; now if a friend want to do a search in Wikipedia for cats, the edited page should show up instead of original page.Is there a way to do this using java script.
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?
How to check if a web page is synchronized on a local server in JSP ? I have tried to make database updation synchronized in jsp but how can i check it on local web server (tomcat) ? I have used Oracle database.
More Info:
Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection("jdbcracle:thin:@localhost:1521:xe","username","password"); balance=balance-1500; String query1= "update bank set bal=? where card_no='"+cnumber+"'"; PreparedStatement st1 = con.prepareStatement(query1); st1.setString(1,String.valueOf(balance)); int qresult1 = st1.executeUpdate();
This is some of the code in which I have not applied any synchronization. In simple words, I want to know how to test if synchronization part is really working or not ?
My main motive is... I am trying to make an admission form and I want a limited number of seats but I want after every registration process my database updates the seat numbers accordingly and show the right information about how many seats are left to the user. (though in the given code i am just changing the balance)
Like in this example <%! PreparedStatement pst = con.prepareStatement("query"); %> synchronization <% synchronized(pst) { pst.setXXX(...); pst.setXXX(...); pst.executeXXX(...); } %>
Basically this code is supposed to create an int array of 50 elements, then pull the elements listed in positions 1-6 to make a lottery draw.
Problem is it gets down to that last "For" statement and then says "duplicate local variable i." If I attempt to separate it from the previous for loop with curly braces, then it gets mad and says it doesn't know what "local variable i" even IS. So I tried changing it to "j" in the last statement and it couldn't recognise that either. I feel like I need to change the variable name in that second for loop but I'm not sure how to make it understand that the second variable is going to be outputting the values from the first variable.
public class Lottery { public static void main(String[] args) { // TODO Auto-generated method stub int []nums = new int [50]; for (int i = 1; i <50; i ++) {nums[i] = i;}
I would like to ask how i can create a bidirectional folder copy system with SFTP JSch is there any example. Like i see the code can only transfer file, I need to transfer a folder with many files from my pc to a server and the opposite.
I'm trying to create a server which sends the clients connected to it its local time. Looking at a few tutorials I've managed to connect the clients to the server, but can't send data to the clients. I've successfully done easier examples, without threading. I guess the problem might be im me not knowing what exceptions are for.
Client: When running the code "AAAAAAA" does execute but "BBBB" doesn't, so I guess the problem should be in fraseRecibida = entradaDesdeServidor.readLine();
import java.io.*; import java.net.*; import java.util.Scanner; public class Client { public static void main(String[] args) throws Exception{ String fraseRecibida;
[code]....
I don't understand the exceptions, maybe I should give them a look before continuing with sockets. Being frank I'm not really sure why the while(true) is there.
import java.io.*; import java.net.*; import java.util.Calendar; public class ServerThread extends Thread{ Socket socket; ServerThread(Socket socket){ this.socket = socket;
I've generated SOAP classes via wsimport from a local WSDL. All of the URLs in the WSDL point to the production services, but for testing I need to connect to a test location (different URL). There are dozens of examples on the web to set the endpoint, but it doesn't seem to be working for me - it's hitting the prod URL, not test.
PrepaidServices ps = new PrepaidServices(); ps.setHandlerResolver(new CustomHandlerResolver()); IPrepaidServices port = ps.getPrepaidServices(); BindingProvider bp = (BindingProvider) port; bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, TEST_URL); LOG.debug("New Port = " +
[Code]...
The debug correctly shows the TEST port being set correctly, but when executed, inside one of the handler resolvers, I have the following code: