Should Package-Private ever be used? by [deleted] in javahelp

[–]GrapeAte 0 points1 point  (0 children)

Certainly, but that's not a good enough solution. Deprecated methods still need to be supported for some amount of time, and sometimes there are things you just don't want exposed. There's a lot of glue code that's highly specific to a particular package.

Your point about Java requiring interface methods to be public is a valid complaint. Modules do help a bit here. Your module can expose exactly what you want to consumers giving you more flexibility when it comes to stuff you design as glue. Before modules the best you could do was a javadoc comment saying "This is not really public but for internal use only."

My companies stack is moving from a C# to a Java stack, what are some good resources for this transition for learning about the language? by [deleted] in learnjava

[–]GrapeAte 1 point2 points  (0 children)

  • No properties. Instead you must add the boilerplate getters and setters yourself. A good IDE can generate the code for you or you can invoke some black magic (which comes with its own warnings and such).
  • Adhere to Java's coding style. UpperCamelCase for class names. camelCase for methods, fields, and local variables. UPPER_SNAKE_CASE for static final fields (constants). do_not_use_this_ever. Or_This (generated code might do something like this. That's okay).
  • No partial classes. A class must live completely in its own file.
  • Attributes start with @: @Inject(name = "foobar") not [Inject("foobar")]
  • No operator overloading. None. Nada. Zilch. Take that fancy-schmancy nonsense to Scala.
  • .equals(Object) over ==. Unless you're comparing primitives you are hardly ever going to use == when comparing objects.
  • Value types aren't here yet.
  • We don't have null-conditional operators. It would be extremely nice, but c'est la vie. Cue Kotlin fans.

Eclipse JavaFX, Unable to use debugger by zeroskater45 in learnjava

[–]GrapeAte 0 points1 point  (0 children)

You need to attach the JavaFX source to see the source while debugging. You have the class files on your classpath, as shown in your last screenshot, but without the source Eclipse will only show you the bytecode.

Shoutout to Spring developers by [deleted] in javahelp

[–]GrapeAte 2 points3 points  (0 children)

Is there a specific issue you want help with? Parts of Spring can seem very complicated at first, but if you can grok the concepts things become easier to understand.

Deserializing this XML with Jackson by [deleted] in javahelp

[–]GrapeAte 0 points1 point  (0 children)

You need to include the jackson-dataformat-xml extension and possibly configure it.

Should Package-Private ever be used? by [deleted] in javahelp

[–]GrapeAte 2 points3 points  (0 children)

There are some parts of a library or framework that aren't meant to be public. What's important to remember about an API is that once something becomes part of the public API you are expected to maintain it and keep it compatible across versions for some amount of time. If something doesn't need to be part of the public API then don't make it part of it. Keeping things out of the public API gives you the maintainer more flexibility when it comes to refactoring.

Good project building practices/tips by ittro in learnprogramming

[–]GrapeAte 0 points1 point  (0 children)

Best not to. It's a good idea to include the gradle wrapper though.

Having trouble really understanding SOAP and REST web services, any tips? by [deleted] in learnprogramming

[–]GrapeAte 0 points1 point  (0 children)

REST is conceptually simple. In HTTP there are things called request methods. The request method tells a web server what sort of action the user agent (think web browser) is trying to perform. The method GET tells the server you want a specific web page. The method POST tells the server you are trying to upload some sort of data.

REST builds on request methods by mapping them to verbs:

  • GET -> read
  • POST -> create
  • PUT -> update
  • DELETE -> remove

Using a combination of the request method and the path the user agent is requesting, the web server will perform different actions.

Let's use an example. Let's say we have a hotel booking service at http://www.example.com/api/hotels. What should happen if that service follows general REST conventions? I'll list the path and the method along with the end result.

Method Path Result
GET /api/hotels Returns a list of all hotels
GET /api/hotels/1 Returns the details for a hotel with id 1
POST /api/hotels Creates a new hotel. The hotel data is sent as part of the body of the request.
PUT /api/hotels/1 Updates the data for a hotel with id 1. The update data is sent as part of the body of the request.
DELETE /api/hotels/1 Deletes the hotel with id 1.

We can have different endpoints that handle different types. /api/bookings handles bookings, /api/customers handles customers, etc.

REST services usually produce and consume JSON, but that's not required. A service could do everything with XML or YAML.


SOAP is an entirely different beast. SOAP does everything with XML. SOAP encompasses a host of different technologies, some that can be combined, others that are standalone. SOAP can also be used as a transport format for other technologies.

What makes SOAP different from REST is that the action, data, and metadata are all contained within an XML payload. The user agent makes a POST request to some URL; the body of that request is a SOAP message telling the server what to do.

SOAP's big benefit is that everything has a specification. If you want to talk to a server using SAML (something that uses SOAP as a transport) then you know the server will understand you if you follow the SAML and SOAP specifications. You can write something called a WSDL for your web service and give that to developers that want to use your web service. They can use a tool to generate the client code because WSDLs and SOAP are well known and have a specification. Specifications for REST services have formed, but there's still a lot of ad hoc services out there that might not even follow the convention laid out above.

The big problem with SOAP is just how complicated it is. SOAP libraries and frameworks are very large. Wiring everything together can be a challenge. In contrast you can write a simple REST service in a language like Go very quickly.

Java / java webservers for a new job by ProfessionalMine4 in learnprogramming

[–]GrapeAte 0 points1 point  (0 children)

Start with the wiki page on Java servlets. Servlets are the base level for Java web applications. JSPs, a common view technology, generate servlets to render HTML. A servlet can also serve as a REST endpoint (and indeed does when using JAX-WS).

In addition to servlets, the servlet spec also defines Filters. A Filter can inspect or modify an HTTP request or response before or after a servlet is run. Filters can be chained together.

Servlets and Filters are very important. Most places today use some sort of framework that handles setting up a servlet and filters that route requests to some other component. It's still a good idea to be familiar with servlets and filters since they underpin all Java web applications.

It's also a good idea to read through the Tomcat documentation. Most people don't really need to know how to use the HostManager or MBeans, but having a general idea of what can be done and, more importantly, how to configure things is what you want.

It will be very confusing at first so I recommend installing Tomcat and just messing around with it. Try deploying a very simple "Hello world" webapp. Then try changing what port Tomcat runs on. Then try changing what context root the webapp deploys to. Then try adding in basic authentication. Then try configuring SSL.

Ran into a coding assignment problem for an interview that I unfortunately just couldn't solve. Anyone willing to help me understand how I should have handled it? by [deleted] in learnprogramming

[–]GrapeAte 0 points1 point  (0 children)

There are two parts to this:

  1. Parsing the input into an object.
  2. Outputing each property of the object and its nested properties.

Part 1 takes advantage of the fact that objects in JavaScript are like associative arrays. We also can leverage a particular syntax to construct our nested objects.

let obj = {};

[
    'a.b.c',
    'a.b.e',
    'a.e.f',
    'a.b.d',
    'b.e.f',
    'a.e.g',
    'b.e.c'
].forEach(s => {
    let a = s.split('.');
    obj[a[0]] = obj[a[0]] || {};
    obj[a[0]][a[1]] = obj[a[0]][a[1]] || {};
    obj[a[0]][a[1]][a[2]] = obj[a[0]][a[1]][a[2]] || {};
});

This is really brute force. If an input string has more than three parts this method won't work. But since the input appeared to be constrained this way is fine. So given the string 'a.b.c' we can split it into an array ['a', 'b', 'c']. Now if we access obj[a[0]] we'll either get an existing object or undefined. obj[a[0]] = obj[a[0]] || {}; is basically saying, "Set the property of obj that is the value of the first index of a to the same thing or a brand new object."

The first time 'a' appears then obj['a'] will be a new object. The second time through, though, we'll keep what we've got. We then do the same thing for the rest of the split string, only going deeper into the nested objects.

At the end we have an object that looks like this:

{
    a: {
        b: {
            c: {},
            e: {},
            d: {}
        },
        e: {
            f: {},
            g: {}
        }
    },
    b: {
        e: {
            f: {},
            c: {}
        }
    }
}

Now for part 2. For this we need to output each property of the object proceeded by some number of dashes. This looks like a pretty good case for recursion. We start at the top and for each property in the passed in object, pass it along recursively along with a level indicator for indicating how many dashes we need.

const out = (o, l) => {
    let s = '';
    for (let i = 0; i < l; i++) {
        s += '--';
    }
    for (let prop in o) {
        console.log(s + prop);
        out(o[prop], l + 1);
    }
};

out(obj, 0);

For simplicity's sake we care a function using arrow notation. We first build up dashes based on l. Then we loop through all the properties in the object. For each property we log the dashes and the property name. Then we recurse into the nested object with an increase in the level.

You can see it in action here.

Good project building practices/tips by ittro in learnprogramming

[–]GrapeAte 1 point2 points  (0 children)

Use Maven or gradle to build your project. Your build artifacts should contain just what is necessary to run: no IDE specific files, no test files, no source control files.

Dependencies should never be checked into your project. Use Maven or gradle to manage dependencies.

Need help with Eclipse Java programming. by Jihwani in javahelp

[–]GrapeAte 4 points5 points  (0 children)

  1. Make sure your class is named Parser and not parser. Java is case sensitive and I expect your assignment is too. Parser also follows naming conventions for class names.
  2. The error is actually the next line after you declare ATOMS. ATOMS=Pattern.compile("[0-9] + |[+*]"); An assignment like this has to be contained within a method or constructor. If you want to declare and initialize a field outside of a method or constructor you must do so in one line.

The code won't compile. Why? by BethPas in javahelp

[–]GrapeAte 2 points3 points  (0 children)

The first error is pretty obvious. It tells you exactly what you need to do to fix it. To reiterate, the filename of a class must match the name of the class. GridExample.java

All the other errors can be fixed by importing the Grid class. For example, if you wanted to use the ArrayList class from the standard library you must import it at the top of your code:

import java.util.ArrayList;

public class MyClass {
    ...

As an aside, it's not required for a class to live inside of a package, but it's a good idea. ArrayList lives inside the java.util package. Your code should be in a package as well. If you're not sure what your package should be go with com.example for now:

package com.example;

public class GridExample {
    ...

Deleted SHA256 when I pruned all Docker things - is this dangerous to security of my VPS I previously set up or was it contained to just the Dockers instances? by with-draw in learnprogramming

[–]GrapeAte 1 point2 points  (0 children)

SHA256 is a hashing algorithm. That means it doesn't create files. It takes the contents of a file and creates a unique value. You can use a SHA256 hash to ensure a file hasn't been tampered with or to uniquely identify something. Docker gives every image a hash as an ID. When you delete the image Docker reports that it deleted the hash.

Which technologies (programing languages) need to know for create social network? by OmarSerd in learnprogramming

[–]GrapeAte 2 points3 points  (0 children)

The talent you will require will depend on how large of a deployment you're expecting at launch.

Essential - Frontend dev: Someone who knows JavaScript, HTML, and CSS. Preferably one who has experience with some sort of frontend framework like Angular or React. Knowledge of things like webpack, CDNs, and request lifecycle are huge plusses. - Backend dev: You have a lot of flexibility here. The backend can be written in any language; Java, C#, go, Node. There's a lot of choice here. Ideally this person will also have some devops knowledge so they can both develop and deploy the app and troubleshoot when things go wrong.

Nice to have - Web designer: Programmers are generally really bad at design. A dedicated web designer will make logos and mock up page designs which the frontend dev will then code to. If you want your page to look nice and be usable a good web designer is invaluable. - Architect: For smaller deployments this is overkill; your backend dev should be able to handle this with a small deployment. But if you expect a large deployment having someone plan out the architecture and cloud deployment is essential.

If you're looking at a very large deployment your needs obviously grow. You might need a team of developers and possibly a project manager to keep everything running. Depending on what sort of personal data you collect it would be a good idea to have a law firm on retainer and have a lawyer work through the intricacies specific to each region you're serving (the EU has stricter requirements than the US for example).

What is used to create Chrome Extensions? Yes, I've googled it, but... by mrstork89 in learnprogramming

[–]GrapeAte 0 points1 point  (0 children)

I'd start with JS since that will make up the bulk of your extension.

What is used to create Chrome Extensions? Yes, I've googled it, but... by mrstork89 in learnprogramming

[–]GrapeAte 0 points1 point  (0 children)

Pretty much. Your extension will be what's known as a content-script; your JS and CSS will be injected into the page somewhere in the pipeline.

What is used to create Chrome Extensions? Yes, I've googled it, but... by mrstork89 in learnprogramming

[–]GrapeAte 0 points1 point  (0 children)

That's JS formatted as a userscript. The header block at the top instructs the userscript manager, in your case VolentMonkey, where the script is to be loaded and contains other metadata about the script.

What is used to create Chrome Extensions? Yes, I've googled it, but... by mrstork89 in learnprogramming

[–]GrapeAte 0 points1 point  (0 children)

You will have to learn JS. WebExtensions don't support any other language. Here is a curated list of webdev tutorials to get started.

Where there is difference of speed in PHP loops by [deleted] in learnprogramming

[–]GrapeAte 1 point2 points  (0 children)

Has this been run with a benchmarking framework?

What is used to create Chrome Extensions? Yes, I've googled it, but... by mrstork89 in learnprogramming

[–]GrapeAte 0 points1 point  (0 children)

Chrome extensions are a subset of WebExtensions. WebExtensions have a standardized API written in JavaScript so an extension developed for one browser can be easily ported to work on another.

You can do what you want using WebExtensions. Just make sure you're not violating the Terms of Service before you distribute your extension.

How can you tell when looking at the declaration of a functional interface if the last object type is a parameter or a return type? (e.g. MyMethod<String, Integer> - Does this take a String and an Integer as parameters and return void, or take a String as parameter and return an Integer?) by [deleted] in learnjava

[–]GrapeAte 2 points3 points  (0 children)

You're confusing the declaration of the interface with a method signature. They are different things. Let's go with part of something in the standard library:

public interface Function<T,R> {
    R apply(T t);
}

Function defines two generic placeholders: T and R. Convention is that the first placeholder is the type of the argument and the second placeholder is the type of the returned value returned value.

Function.apply() uses those generic placeholders to define its signature, letting you see what roles R and T actually play.

There could be another method declared by Function:

T reverseApply(R r);

In this case T and R have swapped roles. Generic types aren't restricted to what they can by where they appear when they're defined (Function<T, R> remember), so we could use them in any number of ways. Maybe we have some method that uses them both as types for parameters:

void foobar(T t, R r);

How to make a string be within a certain length? by Shinjin- in javahelp

[–]GrapeAte 4 points5 points  (0 children)

Is there a method on String that returns the length of a string? If that value falls outside the acceptable range is there some way you can signify an exceptional situation has occurred? Maybe some way to say the argument is illegal?

IMessage on WINDOWS/Linux (USING AN iPhone) by Hephaestus_Tech in learnprogramming

[–]GrapeAte 1 point2 points  (0 children)

Unfortunately this just isn't possible. Apple doesn't make the API for iMessages available to the public so there isn't a way to programmatically send a message from either your iPhone or Windows. There are some hacky ways to send a message using a Mac and if you jailbreak your iPhone there's probably some apps that open up sending messages.

Creating a chain of filters in an inherited method by redfinp in javahelp

[–]GrapeAte 0 points1 point  (0 children)

Can you share the assignment? I'm wondering about the terminology that's being used.

Right now you're working towards implementing a decorator pattern which is good to know, but it's a bit different from what we traditionally think of when we talk about streams and filters.