Java Type Promotion Query - Int Cannot Be Assigned To Byte
Oct 14, 2014
I've a small question relating to type promotion I can't find an answer for on the web. Basically in your code if you have :
byte b = 0;
b = b + 1;
The compiler will complain about the result being an int which cannot be assigned to a byte. That I understand, as b on the right hand side of the expression is promoted to an int and the result of the addition is an int. However the following does compile :
byte b = 0;
b++;
Does the post increment not carry out the post increment as "give me the value of b and then add 1 to b" where I would have expected 'add 1 to b' to do the same integer promotion as the previous example ? The compiler will also allow the following
I am reading a book on Java and we are at a point where it is explaining type conversion in expressions. One of the examples shared has a byte being multiplied by itself and then assigned back to itself ...
byte b; b = 10; b = (byte) (b * b);
this is all good and dandy (that is, the code functions properly).
However, I am confused why I need to typecast here! Without the cast, the compiler screams, "Type mismatch: cannot convert from int to byte." Yet I haven't converted to an int?? It appears there was an implicit conversion.
The final value, 100, is clearly within byte's range of -127 to +127 isn't it? So I am lost as to what is the issue here.
I am having a problem with a PreparedStatement for a MySQL query, where the query seems to get cut off. I can't really post a self contained example, because it involves an SQL database.The part of the code with the problem...
Java Code:
public void insertEntry( Hashtable<String, String> strings, Hashtable<String, Integer> integers, Date created, Date paid, boolean enabled) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver");
[code]....
The output of the whole program, though all it has done so far is create some default values and attempt to insert them with the above code, where the problem occurs.
Debug: SQLpaid = 1990-03-21
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT enabled=0, username='default_username', created='2000-03-21', paid='1990-' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:45)
I seem to be having a issue with my java console app what it should do is query a SQL DB and list the user details (userid, firstname ,lastname) from the user table, and the users stocks (stockname ,stockdiscription) associated to each user from the userstocks table if I'm thinking correctly!
I'm sure it is just a logical error, but I'm not sure of the way to get the out put I'm looking for. I will add the makeDB.java file and DatabaseQuery.java file as well as the current Exception(Run Time Exception) below.
**I have added other users and stocks manually - Not just what the MakeDB.java file inserts **
Output and Exception:
Stock holdings by User User ID User Name Stock - Description ------------------------------------------- admin01 Default Admin Exception in thread "main" java.sql.SQLException: ResultSet is closed at sun.jdbc.odbc.JdbcOdbcResultSet.checkOpen(JdbcOdbcResultSet.java:6647 ) at sun.jdbc.odbc.JdbcOdbcResultSet.next(JdbcOdbcResultSet.java:1248) at DatabaseQuery.main(DatabaseQuery.java:45) Press any key to continue . . .
Java Code:
MakeDB.java: import java.sql.*; import java.io.*; public class MakeDB { public static void main(String[]args) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
okay so it says that java int short and byte variables are the same thing. They take whole numbers. But what is the point of byte and short to even exist if int covers it all? Is the short and byte just for fun?
I have code which query value from database, the use case is the user can enter value as 1,01,11 in database but when the user enter value in xml file he can only enter 11,01,12 in database there is two columns lets say column test1=1 and test2=2 combination of this is 12, which is the value the use will enter in xml, but sometime the use can enter test1=04 than column test2=00 how can i approach this
I got table called card with column crdind,crdpos1,cardpos2 as primary key. The user can enter value as
The rule say if you enter one number in crdpos1 eg 2 than you must enter value in crdpos2 it can be any number 1-9 but if you enter 00-09 in crdpos1 than crdpos2 is 00
The challenge I have is to query this value after they been enter. I have xml file which got field crdpos the use enter 15 which is the combination of column crdpos1=1 and crdpos2=5 ... How can I split the value crdpos and got and able to query in database in two separate column ....
I am working on descending after I assign values. And everything is sorted, but one value. I don't know why always I get one value is not sorted.
public class SeunghunJunHw6 { public static void main(String[] args){ double[] randomArray = new double[9]; //assign random double values between 0 to 100 but exclude 100 for(int i = 0; i<randomArray.length;i++){ randomArray[i] = Math.random()*100;
But, I coudln't find any place in Java/JSP where the value for status is being set. What could be the possible place where the values for status is being set.
As the code is client specific, so, I couldn't paste the specific code over here but I have searched in whole workspace i couldn't find a single place where values for status is being set/assigned to.
I have embed a query in the JAVA to retrieve the data from the back end and I use Oracle for back end. What I'm trying to do is in my query has been shown below. The issue here is that result set returns the NULL. But fine when I change the query "SELECT 5 FROM DUAL" and it returns 5 for me which right. When I try to run the same at the back end as a query I'm getting proper result.
public String getReturnPart(String partNumberIn) { //int returnPartNumber = 0; String query = "SELECT COUNT(*) AS CHECK1 FROM PART_MASTER PM,UNITS_OF_MEASURE UOM WHERE PM.PART_NUMBER = '" + partNumberIn + "' AND PM.UNIT_OF_ISSUE = UOM.UNIT_OF_MEASURE AND UOM.UNIT_TYPE = 0 "; //String query = "SELECT 5 AS CHECK1 FROM DUAL"; This returns fine as "5" ResultSet result = null;
I and a friend are working with a project to create a file system, who manages a secondary memory simulated as a byte array in Java. We want the file system to be a hierarchical tree structure like in UNIX.
We have come quite far, but the paths are not handled correct. I seem to have mistaken the relative folder ./ for the root folder, but it should mean "working directory folder", ie, where I stand now. That is, if I stand in /dir1 as my "working directory" and make mkdir ./dir2 then should dir2 end up as subfolder in dir1. But with me it appears in the root.
Is there an advantage in using byte instead of int beyond the space savings? In my program, I'll never need close to the max value of a byte, let alone int, so it seems like a waste to make my primitives ints.
I need to send a byte array across a network. I know how to do this. (server->client)
byte[] myArray = new byte[]{0,1,2,3,4,5,6,7,8,9}; DataOutputStream.write(myArray);
... and I know how to receive it.
byte[] myArray = new byte[10]; DataInputStream.read(myArray);
When I send over one of these arrays, it ends up stopping storing values in the array when only about half the array is received, even though the array is sent from the server all at once. This results in the receiver's array, in this case, being something like {1,2,3,4,0,0,0,0,0,0}.
I can easily solve this - and already have - by simply adding a loop onto it and waiting for the bytes to all be received, as the method returns the amount of bytes actually read.
I am simply wondering if there is a better solution to this, as the current solution isn't that elegant. Did I do something wrong to cause only a part of the array to be sent first? Would it be better to use DataInputStream.readByte() to read off the bytes one by one rather than an array at once, and then store them in an array afterward? Would this cause a performance decrease as each byte is read individually? (I'm sending an array of several million bytes)
I am new to Android. I have byte array of size 10. I am passing the Decimal values (131 - 140) to byte array. But while printing I get Negative (-) values with decreasing order .
How can I get same value as positive values?
Or How can I store positive value e.g. 131 as byte array element.
Please not my requirement is array must be ByteArray only
I have a checksum function that is suppose to read IPV4 packet and return a short integer value. The IPV4 packets are stored in a byte array. I am having trouble storing the first 8 bits and second 8 bits of the short integer into the byte arrays especially when they have leading 1s. For example, if my checksum returns 5571 (binary = 0001 0101 1100 0011) The first 8 bits is suppose to represent 195 but when I try to assign a larger integer type to a btye the information gets sign extended. The 195 turns into -61. I tried using bit addition like array[10] = array[10] & 0xff, but the result remains the same.
public static short checksum(byte [] a, int length) { short sum = 0; long data; int i = 0; while(length > 1) { data = (((a[i] << 8) & 0xff00) | ((a[i + 1]) & 0xff)); sum += data;
I'm trying to create Web Services with Eclipse (Java Runtime 7 (also tried 8) Tomcat 7 (also tried 8).Web Service with parameter "byte[]" and return value "byte[]" works fine.Web Service with parameter "myOwnClass" and return value "myOwnClass" works also fine.But if I have a "byte[]" element in "myOwnClass" and I run my Client test program I get the following error:
AxisFault faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException faultSubcode: faultString: java.lang.NumberFormatException: For input string: "MTExMTExMTExMQ=="
Why? I don't have any numeric element (such as int ...) in my class members!?
If you have final int i = 1; short s = 1; switch(s) { case i: System.out.println(i); }
it runs fine. Note that the switch expression is of type short (2 bytes) and the case constant is of type int (4 bytes).My question is: Is the type irrelevant as long as the value is within the boundaries of the type of the switch expression?I have the feeling that this is true since:
byte b = 127; final int i = 127; switch(b) { case i: System.out.println(i); }
This runs fine again, but if I change the literal assigned to i to 128, which is out of range for type byte, then the compiler complains.Is it true that in the first example the short variable and in the second example the byte variable (the switch expressions) are first implicitly converted to an int and then compared with the case constants?
Got a problem with generics, which I'm still pretty new at. Here's a program that compiles fine:
import java.util.ArrayList; import javax.swing.JComponent; public class Experiments { public static void main(String[] args) { ListHolder holder = new ListHolder();
[Code] ....
It's useless, but it compiles. If I change Line 14, however, to add a generic type parameter to the ListHolder class, Line 10 no longer compiles:
import java.util.ArrayList; import javax.swing.JComponent; public class Experiments { public static void main(String[] args) { ListHolder holder = new ListHolder();
[Code] ....
I get this error:
Uncompilable source code - incompatible types: java.lang.Object cannot be converted to javax.swing.JComponent at experiments.Experiments.main(Experiments.java:10)
Apparently, the introduction of the type parameter leaves the compiler thinking that aList is of type Object. I can cast it, like this:
JComponent c = ((ArrayList<JComponent>)holder.aList).iterator().next();
That makes the compiler happy, but why is it necessary? How does adding the (unused) type parameter to the ListHolder class end up making the compiler think the aList member of an instance of ListHolder is of type Object?