I don't understand wrapper classes
Integer b = new Integer(8); b=b+1; System.out.println(b);
here I added 1 to b and I got 9 on the console, I could change the Integer object within the main method
You're not actually "changing" the Integer object here. What you're doing is creating a new Integer, with a value of 9, and then storing it in the b variable, throwing away the old object which had the value 8.
i.e. you're changing the variable to point to a new object, rather than modifying the object in-place.
Modifying / mutating the original object would look something like this - it's not something valid which you can do:
Integer b = new Integer(8); b.increaseBy(1); System.out.println(b);
The difference is subtle, but it also explains why the code in 'main' couldn't "see" the changes made in the 'add' method.
I also used valueOf and parseInt methods, valueOf method returns an Integer object but I assigned an int primitive type to it and it worked. Similarly, I declared an Integer object and assigned it to the result of parseInt method which returns a primitive type and it also worked.
Basically Java will automatically convert between Integer and int for you, based on what's required in any given context.
This is called "automatic boxing / unboxing".
The main reason Integer exists is:
-
It's possible to have a 'null' Integer, but not a null int - if you want to allow a null value, you need to use the object wrapper type
-
Collections like List, Set, Map etc do not support primitive types, only objects - so these object wrapper / boxed versions need to exist to allow you to store primitive values in collections
Any reason to use a class instead of a primitive when declaring fields?
Why can't you make ArrayLists of primitive types...and why can you "cheat" around the restriction by using the wrapper classes?
Java Database Wrapper
If you search for "java jdbc simple DAO" you might be able to borrow some ideas from that. You can blow allot of time with it though so I would set a time limit on how much your research.
I am assuming your are using jdbc. One thing that might be useful is to test database calls outside of the web app so you get all the bugs out first before using it in a web app. ie make a simple java Application using your database classes to call the database directly.
More on reddit.com